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.
My JQuery approach using an extension method:
You can use it like this:
And it renders like this:
For each submit button just add:
Based on mkozicki answer I come up with a bit different solution. I still use
ActionNameSelectorAttribute
But I needed to handle two buttons 'Save' and 'Sync'. They do almost the same so I didn't want to have two actions.attribute:
view
controller
I also want to point out that if actions do different things I would probably follow mkozicki post.
I don't have enough rep to comment in the correct place, but I spent all day on this so want to share.
While trying to implement the "MultipleButtonAttribute" solution
ValueProvider.GetValue(keyValue)
would incorrectly come backnull
.It turned out I was referencing System.Web.MVC version 3.0 when it should have been 4.0 (other assemblies are 4.0). I don't know why my project didn't upgrade correctly and I had no other obvious problems.
So if your
ActionNameSelectorAttribute
is not working... check that.I've created an ActionButton method for the HtmlHelper. It will generate normal input button with a bit of javascript in the OnClick event that will submit the form to the specified Controller/Action.
You use the helper like that
this will generate the following HTML
Here is the extension method code:
VB.Net
C# (the C# code is just decompiled from the VB DLL, so it can get some beautification... but time is so short :-))
These methods have various parameters, but for the ease of use you can create some overload that take just the parameters you need.