Is there some easy way to handle multiple submit buttons from the same form? Example:
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" value="Send" />
<input type="submit" value="Cancel" />
<% Html.EndForm(); %>
Any idea how to do this in ASP.NET Framework Beta? All examples I've googled for have single buttons in them.
You should be able to name the buttons and give them a value; then map this name as an argument to the action. Alternatively, use 2 separate action-links or 2 forms.
If your browser supports the attribute formaction for input buttons (IE 10+, not sure about other browsers) then the following should work:
I've tried to make a synthesis of all solutions and created a [ButtenHandler] attribute that makes it easy to handle multiple buttons on a form.
I've described it on CodeProject Multiple parameterized (localizable) form buttons in ASP.NET MVC.
To handle the simple case of this button:
You'll have something like the following action method:
Notice how the name of the button matches the name of the action method. The article also describes how to have buttons with values and buttons with indexes.
You could write:
And then in the page check if the name == "Send" or name == "Cancel"...
Here is what works best for me:
Eilon suggests you can do it like this:
I like this method as it does not rely on the value property of the submit buttons which is more likely to change than the assigned names and doesn't require javascript to be enabled
See: http://forums.asp.net/p/1369617/2865166.aspx#2865166