I need multiple submit buttons to perform different actions in the controller.
I saw an elegant solution here: How do you handle multiple submit buttons in ASP.NET MVC Framework?
With this solution, action methods can be decorated with a custom attribute. When the routes are processed a method of this custom attribute checks if the attribute's property matches the name of the clicked submit button.
But in MVC Core (RC2 nightly build) I have not found ActionNameSelectorAttribute
(I also searched the Github repository). I found a similar solution which uses ActionMethodSelectorAttribute
(http://www.dotnetcurry.com/aspnet-mvc/724/handle-multiple-submit-buttons-aspnet-mvc-action-methods).
ActionMethodSelectorAttribute
is available but the method IsValidForRequest
has a different signature. There is a parameter of type RouteContext
. But I could not find the post data there. So I have nothing to compare with my custom attribute property.
Is there a similar elegant solution available in MVC Core like the ones in previous MVC versions?
Best Solution
You can use the HTML5
formaction
attribute for this, instead of routing it server-side.Then simply have controller actions like this:
A good polyfill for older browsers can be found here.
Keep in mind that...
ModelState
or otherwise - occurs on the action that was posted too, it will need to send the user back to the correct view. (This is not an issue if you are posting through AJAX, though.)