ASP.Net MVC 4 Form with 2 submit buttons/actions

前端 未结 7 1501
故里飘歌
故里飘歌 2020-11-28 21:45

I have a form in ASP.Net and razor.

I need to have two ways of submitting said form: one that goes through the Edit action, and another that goes throug

相关标签:
7条回答
  • 2020-11-28 22:18

    That's what we have in our applications:
    Attribute

    public class HttpParamActionAttribute : ActionNameSelectorAttribute
    {
        public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
        {
            if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
                return true;
    
            var request = controllerContext.RequestContext.HttpContext.Request;
            return request[methodInfo.Name] != null;
        }
    }
    

    Actions decorated with it:

    
    [HttpParamAction]
    public ActionResult Save(MyModel model)
    {
        // ...
    }
    
    [HttpParamAction]
    public ActionResult Publish(MyModel model)
    {
        // ...
    }
    

    HTML/Razor

    @using (@Html.BeginForm())
    {
        <!-- form content here -->
        <input type="submit" name="Save" value="Save" />
        <input type="submit" name="Publish" value="Publish" />
    }
    

    name attribute of submit button should match action/method name

    This way you do not have to hard-code urls in javascript

    0 讨论(0)
  • 2020-11-28 22:18

    You can do it with jquery, just put two methods to submit for to diffrent urls, for example with this form:

    <form id="myForm">
        <%-- form data inputs here ---%>
        <button id="edit">Edit</button>
        <button id="validate">Validate</button>
    </form>
    

    you can use this script (make sure it is located in the View, in order to use the Url.Action attribute):

    <script type="text/javascript">
          $("#edit").click(function() {
              var form = $("form#myForm");
              form.attr("action", "@Url.Action("Edit","MyController")");
              form.submit();
          });
    
          $("#validate").click(function() {
              var form = $("form#myForm");
              form.attr("action", "@Url.Action("Validate","MyController")");
              form.submit();
          });
    </script>
    
    0 讨论(0)
  • 2020-11-28 22:19

    We can have this in 2 ways,

    Either have 2 form submissions within the same View and having 2 Action methods at the controller but you will need to have the required fields to be submitted with the form to be placed within

    ex is given here with code Multiple forms in view asp.net mvc with multiple submit buttons

    Or

    Have 2 or multiple submit buttons say btnSubmit1 and btnSubmit2 and check on the Action method which button was clicked using the code

    if (Request.Form["btnSubmit1"] != null)
    {
     //
    }
    if (Request.Form["btnSubmit2"] != null)
    {
     //
    }
    
    0 讨论(0)
  • 2020-11-28 22:21

    If you are working in asp.net with razor, and you want to control multiple submit button event.then this answer will guide you. Lets for example we have two button, one button will redirect us to "PageA.cshtml" and other will redirect us to "PageB.cshtml".

    @{
      if (IsPost)
        {
           if(Request["btn"].Equals("button_A"))
            {
              Response.Redirect("PageA.cshtml");
            }
          if(Request["btn"].Equals("button_B"))
            {
              Response.Redirect("PageB.cshtml");
            }
      }
    }
    <form method="post">
       <input type="submit" value="button_A" name="btn"/>;
       <input type="submit" value="button_B" name="btn"/>;          
    </form>
    

    0 讨论(0)
  • 2020-11-28 22:33

    Here is a good eplanation: ASP.NET MVC – Multiple buttons in the same form

    In 2 words:
    you may analize value of submitted button in yout action
    or
    make separate actions with your version of ActionMethodSelectorAttribute (which I personaly prefer and suggest).

    0 讨论(0)
  • With HTML5 you can use button[formaction]:

    <form action="Edit">
      <button type="submit">Submit</button> <!-- Will post to default action "Edit" -->
      <button type="submit" formaction="Validate">Validate</button> <!-- Will override default action and post to "Validate -->
    </form>
    
    0 讨论(0)
提交回复
热议问题