Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a mostly clean attribute-based solution to the multiple submit button issue based heavily on the post and comments from <a href="http://blog.maartenballiauw.be/post/2009/11/26/supporting-multiple-submit-buttons-on-an-aspnet-mvc-view.aspx" rel="noreferrer">Maartin Balliauw</a>.</p> <pre><code>[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class MultipleButtonAttribute : ActionNameSelectorAttribute { public string Name { get; set; } public string Argument { get; set; } public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) { var isValidName = false; var keyValue = string.Format("{0}:{1}", Name, Argument); var value = controllerContext.Controller.ValueProvider.GetValue(keyValue); if (value != null) { controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument; isValidName = true; } return isValidName; } } </code></pre> <p>razor:</p> <pre><code>&lt;form action="" method="post"&gt; &lt;input type="submit" value="Save" name="action:Save" /&gt; &lt;input type="submit" value="Cancel" name="action:Cancel" /&gt; &lt;/form&gt; </code></pre> <p>and controller:</p> <pre><code>[HttpPost] [MultipleButton(Name = "action", Argument = "Save")] public ActionResult Save(MessageModel mm) { ... } [HttpPost] [MultipleButton(Name = "action", Argument = "Cancel")] public ActionResult Cancel(MessageModel mm) { ... } </code></pre> <p><em>Update:</em> <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/#using-multiple-handlers" rel="noreferrer">Razor pages</a> looks to provide the same functionality out of the box. For new development, it may be preferable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload