ASP.NET MVC Core/6: Multiple submit buttons

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.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)