Reputation: 8351
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.
Upvotes: 0
Views: 207
Reputation: 62498
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
}
}
Upvotes: 1