Sometimes you may need to handle multiple submit buttons on the same form as as in the following screen shot.

Multiple buttons
Figure 1: Multiple buttons

In the preceding figure we have the three buttons Login, Register and Cancel. Here each button has different functionality. In this way each submit button will post a form to the server but will provide different values of each button.

Procedure

  1. Create a controller with one action method that accepts two parameters, one is for the model and the other is for determining the status of the button click.
    1. [HttpPost]
    2. public ActionResult Index(Login model, string command)
    3. {
    4. if (command=="Login")
    5. {
    6. // do stuff
    7. return RedirectToAction("Home");
    8. }
    9. else if (command=="Register")
    10. {
    11. // do stuff
    12. ViewBag.msg = "You have Clicked Register button";
    13. return View();
    14. }
    15. else if (command=="Cancel")
    16. {
    17. // do stuff
    18. ViewBag.msg = "You have Clicked Cancel Button";
    19. return View();
    20. }
    21. else
    22. {
    23. return View();
    24. }
    25. }
    In the preceding code snippet, assume you clicked on the Login button, then the command parameter will have the values Login, null, null respectively.

  2. Create a View for the preceding controller.
    1. @model MvcMultipleSubmitButtons.Models.Login
    2. @{
    3. ViewBag.Title = "Index";
    4. }
    5. <h2>
    6. Handling multiple submit buttons in MVC </h2>
    7. <h5 style="color: Red">@ViewBag.msg</h5>
    8. <form action="Home/Index" id="myform" method="post" >
    9. //here action name is Index, controller name is Home. So the action path is Home/Index
    10. <table>
    11. <tr>
    12. <td>
    13. UserName
    14. </td>
    15. <td>
    16. :
    17. </td>
    18. <td>@Html.TextBoxFor(m => m.userName)
    19. </td>
    20. <td>
    21. @Html.ValidationMessageFor(m => m.userName)
    22. </td>
    23. </tr>
    24. <tr>
    25. <td>
    26. Password
    27. </td>
    28. <td>
    29. :
    30. </td>
    31. <td>@Html.TextBoxFor(m => m.password)
    32. </td>
    33. <td>
    34. @Html.ValidationMessageFor(m => m.password)
    35. </td>
    36. </tr>
    37. </table>
    38. <br/>
    39. <div style="padding-left: 80px;">
    40. <input type="submit" id="Login" value="Login" name="Command" title="Login" />
    41. <input type="submit" id="Register" value="Register" name="Command" title="Register" />
    42. <input type="submit" value="Cancel" name="Command" title="Cancel" />
    43. </div>
    44. </form>
    You can declare the form tag in another way as in the following:
    1. @using(Html.BeginForm("Index","Home",FormMethod.Post))
    2. {
    3. //here action name is Index, controller name is Home and form method is post.
    4. }
    Note: there is a relation between button name and action method parameter. For example, the button name is “Command”, the action parameter name should be “command”.

    Command
    Figure 2

    You can have different names for each button. So in that case you need to handle it as in the following:
    1. <input type="submit" id="Login" value="Login" name="Command1" title="Login" />
    2. <input type="submit" id="Register" value="Register" name="Command2" title="Register" />
    3. <input type="submit" value="Cancel" name="Command3" title="Cancel" />
    Controller
    1. public ActionResult Index(Login model, string command1, string command2, string command3)
    2. {
    3. // here command1 is for Login, command2 is for Register and command3 is for cancel
    4. }
  3. Create a Model class with the name Login.
    1. public class Login
    2. {
    3. public string userName { get; set; }
    4. public string password { get; set; }
    5. }

Also the source code has been uploaded with this article, it may help you.

@Happy Coding.