Thursday, February 9, 2012

Multiple Submit button on a single MVC form

Leaving the question of weather this is a good practice or not, there is a way to have multiple submit buttons on a single form. Clicking on any of the buttons will post to the same action, so how do you tell which one was clicked. On your View:
@using (Html.BeginForm())
{
 //other fields here.....
 
 
}
On your controller that the form posts to:
[HttpPost]
public ActionResult Profile(Model model,string submitButton)
{
  Member member = new Member();
  switch (submitButton)
  {
     case "Save1": //do something;
        break;
     case "Save2": //do something;
        break;
  }
   return View(model);
}

No comments:

Post a Comment