I am using ASP.NET MVC 2 with C#. Per the requirements for an application I’ve developed, each view has several buttons that execute the same Controller Post action. Currently, to know what button was clicked when the action was posted, I am using a hidden field on the form called PageAction. This field is set with a value when any button is clicked. This has caused a fair amount of issues in the controller implementation of the POST action for this view because I have to do a case statement against the value submitted via PageAction. It’s one of those things that seems like it could be done better and simpler.
Is there a way to have multiple actions for a single view? This way each button would post the form to it’s own controller action (yet all of these would correlate to the same view). I’d like to get away from the “magic string” checking on the PageAction value; however, all of these buttons are part of the same html form so I’m not sure how this would be possible aside from splitting each button into it’s own form or having each button dynamically change where the form posts to. Both of the latter options sound like they’d be more complicated than what I am already doing.
All suggestions/comments are welcomed. Thanks!
EDIT 1
I’ve gone through the blog post linked here: http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/ and replicated the functionality, but I was wondering…how would this work if the button names on a page are standardized across several different pages in an application? The blog post seems to assume distinct button names matching the action name in the controller, but in my case this won’t be unique enough.
You may find the following blog post useful which illustrates the usage of a custom ActionNameSelectorAttribute.