Which button was clicked in the ASP.NET MVC View?
In an ASP.NET MVC view you might have multiple "submit" buttons inside a <form> such as:
<% using (Html.BeginForm()) {%>
<fieldset>
<p>
<input type="submit" value="Create" name="create" />
<input type="submit" value="Cancel" name="cancel" />
</p>
</fieldset>
<% } %>
The Html helper extension method BeginForm() will emit an opening and closing <form> tag that encloses the input fields.
Your controller that accepts the POST verb will look something like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
string create = Request.Form["create"];
string cancel = Request.Form["cancel"];
return RedirectToAction("Index");
}
catch
{
return View();
}
}
If the Create button was clicked then the create variable will be set to "Create" (because "Create" is the value set to the input named "create") and the cancel button will be set to null. The opposite is true if the Cancel button is clicked.