Multiple submit buttons in a MVC4 view using custom attribute

This post is targeted to anyone who is a beginner in MVC and finds oneself fidgeting over difficulties encountered during its learning curve.  While MVC is a very interesting architecture to work with, I personally felt it really tests one’s patience until you get a hang of it.

One of the common issues that most people face as a MVC novice developer is implementing multiple submit buttons in a form in MVC view. Recently, I had a similar scenario and thought it would a good idea to put it into an article for future reference.

The following three steps elaborate the solution to this problem.

1. We need to create a custom attribute that would match the action name with the name attribute of the button.

3

2. Annotate the action methods with the custom attribute that we just created. These action methods are the ones that need to be executed while submitting the form.

1

2

3. Here is the last step. Simply, specify the form with multiple submit buttons. You should note that the name attribute of these “submit” buttons should match with their corresponding action methods. Also, the “action method” (PostAction) for this form is hardcoded here. This hardcoded value is used in the custom attribute to match the action name. We could create a static property for this purpose to improve the code.

4

References:

Happy Coding! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s