I have one form in ASP.NET MVC (v1) that has 2 input buttons. Each submit button should be contained in this single form, and I need to know which one the user clicked.
I know about this trick to check the FormCollection values that will be returned based on the button clicked. For example, if I have and and the user presses the Button2 button, I should be able to say Request.Form ["Button2"]! = Null, and this will evaluate to true, in which case I know that the user clicked this button.
However, this does not work for me. The values of all my buttons are zero, because they are not contained in the Request.Form values. Is there an error in ASP.NET MVC that swallows these values?
Here is my form code:
<% using (Html.BeginForm()) {%>
<% Html.RenderPartial( "EditAreaControl", Model ); %>
<div class="form-layout-command-container">
<div class="form-layout-command-area-alpha"><button type="submit" name="submit1" value="Save">Save</button></div>
<div class="form-layout-command-area-alpha"><button type="submit" name="submit2" value="SaveAndCopy">Save and Create Copy</button></div>
<div class="form-layout-command-area-beta"><%= Html.ActionLink("Cancel", "list") %></div>
</div>
<% } %>
:
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Add(FormCollection values )
{
if (values["submit1"] != null)
// always false
if (values["submit2"] != null)
// always false as well
}