I have a 4 submit buttons to do different action operation's. For One Submit I have written like this
@using (Html.BeginForm("Edit", "Seller", FormMethod.Post)){
}
How can we do for multiple Submit buttons ? Do we need to write the same for every Submit button?
Here is how you do it for two submit buttons, similary you can do this for 'n' submit buttons.
Below is a form with two submit buttons. Note that both these submit buttons have the same name i.e “submitButton”
Now over to the Controller, the Action takes in an input parameter called string stringButton and the rest is pretty self-explanatory.
I'm using MultiButtonAttribute for this use case. It is nice and clear. You can separate the logic into different action methods.
MulitButtonAttribute
In controller
In view
How it works
It is important that
MultiButtonAttribute
extendsActionNameSelectorAttribute
. When MVC is choosing the right method to match againts the route and it will find such an attribute on method, it will call the methodIsValidName
on attribute passing theControllerContext
. Than it is looking if the key (button name) is in the POSTed form, having not null value (or eventually you can define the expected value of key(button), but it is not necessary).Works pretty well in MVC 2 (but I hope it will work for later versions). Another advantage is that you are able to localize the values of buttons.