Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Core/6: Multiple submit buttons

I need multiple submit buttons to perform different actions in the controller.

I saw an elegant solution here: How do you handle multiple submit buttons in ASP.NET MVC Framework? With this solution, action methods can be decorated with a custom attribute. When the routes are processed a method of this custom attribute checks if the attribute's property matches the name of the clicked submit button.

But in MVC Core (RC2 nightly build) I have not found ActionNameSelectorAttribute (I also searched the Github repository). I found a similar solution which uses ActionMethodSelectorAttribute (http://www.dotnetcurry.com/aspnet-mvc/724/handle-multiple-submit-buttons-aspnet-mvc-action-methods).

ActionMethodSelectorAttribute is available but the method IsValidForRequest has a different signature. There is a parameter of type RouteContext. But I could not find the post data there. So I have nothing to compare with my custom attribute property.

Is there a similar elegant solution available in MVC Core like the ones in previous MVC versions?

like image 256
noox Avatar asked Apr 11 '16 17:04

noox


People also ask

How do I apply multiple submit buttons in one 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 I have multiple submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below. Here I am using MVC VB.net.In view I am using three buttons with the same name but in different values.

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

You can use the HTML5 formaction attribute for this, instead of routing it server-side.

<form action="" method="post">     <input type="submit" value="Option 1" formaction="DoWorkOne" />     <input type="submit" value="Option 2" formaction="DoWorkTwo"/> </form> 

Then simply have controller actions like this:

[HttpPost] public IActionResult DoWorkOne(TheModel model) { ... }  [HttpPost] public IActionResult DoWorkTwo(TheModel model) { ... } 

A good polyfill for older browsers can be found here.

Keep in mind that...

  1. The first submit button will always be chosen when the user presses the carriage return.
  2. If an error - ModelState or otherwise - occurs on the action that was posted too, it will need to send the user back to the correct view. (This is not an issue if you are posting through AJAX, though.)
like image 153
Will Ray Avatar answered Sep 21 '22 22:09

Will Ray