ASP.NET MVC: ActionNameSelector and ActionMethodSelector (or another approach to submit form to multiple action methods) – Part II

0 comments


Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Hope you enjoyed the first part of this blog post.

In this part I’ll discuss about how you can make your own ActionMethodSelector Attribute (though I'll use the out-of-the-box ActionNameSelectorAttribute ActionNameAttribute).

Lets first discuss the problem statement: I’ve one form which has two radio buttons and I want the form to be posted to two different Action Methods based on selection.

ASP.NET MVC: ActionNameSelector and ActionMethodSelector (or another approach to submit form to multiple action methods) – Part I

0 comments

Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

At work I have recently faced a requirement to post a single form to multiple Action methods in a controller based on the context. For example, in the view there is a Form and there are several radio buttons; now my form should be posted to the Action method based on the radio button selection.
As usual I Googled it and found that internet is glutted with various approaches. Some of them are: 1. Posting to single action method and use a switch mechanism inside it, 2. Using multiple forms, 3. Using named submit button and assigning different values to those.
However there is one concept that I liked most and that is making use of ActionNameSelector and ActionMethodSelector.

What are these?
Well these are attributes that influence/affects the selection of action methods… yes you got it right. I’ve quoted it from MSDN and just like some of you (maybe) I didn't understood a thing when I read those lines... ;)
You can check the below MSDN links:
ActionNameSelectorAttribute Class, ActionMethodSelectorAttribute Class.
Hmmm… so what are these anyway.