Using multiple submit buttons in mvc3 razor

In many cases we want to use multiple submit buttons under one Html.BeginForm.
It can be done in easy way.
Lets see the example of it.
Bellow is my view (Index.cshtml) which has multiple submit buttons under single Html.BeginForm.

@{
    ViewBag.Title = "Home Page";
}

@{
    using (Html.BeginForm("ButtonSelect", "Home", FormMethod.Post)) 
    {
      <input type="submit" value="Button1" name="Button"/> <br/>
      <input type="submit" value="Button2" name="Button"/><br/>
      <input type="submit" value="Button3" name="Button"/><br/>
      <input type="submit" value="Button4" name="Button"/><br/>
    }
 }

As we can see in above code, on click of any submit button “ButtonSelect” action of “Home” controller is called.
Also if you see that “name” attribute of all submit buttons are same(it should be same only or else code wont work).
This name will be used in action which is shown bellow.

public ActionResult ButtonSelect(string Button)
        {
            switch (Button)
            {
                case "Button1":
                    return View("Button1");
                    break;
                case "Button2":
                    return View("Button2");
                    break;
                case "Button3":
                    return View("Button3");
                    break;
                case "Button4":
                    return View("Button4");
                    break;
                default:
                    return null;
                    break;
            }

In above code if you see, there is parameter to ActionResult

public ActionResult ButtonSelect(string Button)

Name of this parameter should be same as name attribute of submit buttons.

Using switch case you can do anything you want to do for particular button click.

admin