0

I have an ASP.Net MVC form that submits the viewmodel back to an HttpPost action. I can use the model to get all of the form data as expected but how do I retrieve the name of the submit button (which is not part of the model).

I have two submit buttons, Preview & Save.

John S
  • 7,909
  • 21
  • 77
  • 145
  • possible duplicate of [How do you handle multiple submit buttons in ASP.NET MVC Framework?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – Electric Sheep Oct 23 '14 at 15:01

1 Answers1

1

Give them value and check from Request.Form:

Your form:

@using(Html.BeginForm(.......))
{
 ..................
 ..................
 ..................
<input type="submit" name="SubmitForm" value="Preview"/>
<input type="submit" name="SubmitForm" value="Save"/>
}

and in the action:

[HttpPost]
public ActionResult SomeAction(FormCollection form,ViewModel obj)
{
   if(form["SubmitForm"] == "Preview")
   {
     // Preview Clicked
   }
   if(form["SubmitForm"] == "Save")
   {
     // Save Clicked
   }
}

or:

[HttpPost]
public ActionResult SomeAction(ViewModel obj)
{
 if(Request.Form["SubmitForm"] == "Preview")
 {
   // Preview Clicked
 }
 if(Request.Form["SubmitForm"] == "Save")
 {
   // Save Clicked
 }
}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160