Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net mvc3 razor with multiple submit buttons

I'm using MVC3 Razor. I have 2 submit buttons setup on my view but the problem I'm having is that both submit buttons cause the validation of the model. I want to hook up individual submit buttons with specific input controls for validation.

like image 239
sandeep Avatar asked Apr 18 '11 12:04

sandeep


People also ask

How do you handle multiple submit buttons in the same form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can you have multiple submit buttons in a form MVC?

One Form can do a POST submission to one Action method in Controller and hence in order to use multiple Submit buttons inside one single Form, a Switch case has to be implemented inside the Action method.

Which function is used to handle multiple submit buttons?

Multiple buttons with different names To display a data entry textboxes EditorForModel() helper is used. You can very well use helpers such as TextBoxFor() and LabelFor() if you so wish. There are two submit buttons - one with name attribute set to save and the other with name of cancel.


1 Answers

I know this is a few months old but the solutions here seemed needlessly complex and there's no accepted answer yet. If you name your inputs the same but give them different values, you can get that value in your controller just by including a string with the name of the input as a variable. This is how I solved this problem:

View:

 <input type="submit" id="EnterprisePush" name="btnSubmit" value="Push" />  <input type="submit" id="EnterprisePull" name="btnSubmit" value="Pull" /> 

Controller:

[HttpPost] public ActionResult EnterpriseAdmin(int id, string btnSubmit, FormCollection collection) {   switch (btnSubmit) {     case "Push":       /* Do Something here */       break;     case "Pull":       /* Do Something else here */       break;   } 
like image 172
Rojzik Avatar answered Sep 30 '22 14:09

Rojzik