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.
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.
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.
Happy Coding! 🙂