John S
John S

Reputation: 8351

Accessing a form value that is not part of the viewmodel submitted to an action?

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

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

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

Related Questions