There are times when you need multiple buttons on a single form to complete different functions, such as a simple approval function.
The assumption is that it doesn't have to be discussed with WebForm, but a form in ASP. NET MVC can only be submitted to an action handle, which is a bit more troublesome.
Method One: Use the client script
For example, we write in view:
<inputtype= "Submit" value= "Audit through" onclick= ' this.form.action= "<%=url.action (" Action1 ")%>/>< Inputtype= "Submit" value= "Audit does not pass" onclick= ' this.form.action= "<%=url.action (" Action2 ")%>/>< Inputtype= "Submit" value= "return" onclick= ' this.form.action= "<%=url.action (" Action3 ")%>"/>
When you click the Submit button, change the Action property of the form so that the form is submitted to the action handler for the button.
Sometimes, though, it may be that the logic of Action1 and 2 is very similar, perhaps just to set the value of a field to 1 or 0, then it is a bit redundant to separate into two action.
Method Two: Infer from the action which button commits
In view, we don't have to do any client script processing to add the name attribute to each commit button:
<input type= "Submit" value= "Audit through" name= "action"/><input type= "submit" value= "Audit does not pass" name= "action"/> <input type= "Submit" value= "return" name= "action"/>
Then infer from the controller that:
[HttpPost] Public ActionResult Index (String action/* Other parameters * /) { if (action== "Audit Pass") { // } else if ( action== "Audit does not pass") {// } else { // } }
When I wrote ASP code a few years ago, I used this program frequently ...
View becomes simple, the controller is complex.
Too dependent on view, there are some problems. If one day the customer says the text on the button is changed to "Pass the audit", or is a multi-lingual version, then it is troublesome.
References: http://www.ervinter.com/2009/09/25/asp-net-mvc-how-to-have-multiple-submit-button-in-form/
Method Three: Use Actionselector
The basic principles of actionselector can be seen in this post using Actionselector to control the action selection.
Using this method, we can write the controller like this:
[HttpPost] [Multibutton ("Action1")]public ActionResult Action1 () { // return View ();} [HttpPost] [Multibutton ("Action2")]public ActionResult Action2 () { // return View ();}
In view:
<input type= "Submit" value= "Audit through" name= "Action1"/><input type= "submit" value= "Audit does not pass" name= "Action2"/ ><input type= "Submit" value= "return" name= "Action3"/>
At this point, the controller has no need to rely on the value of the button.
The definition of Multibuttonattribute is as follows:
public class multibuttonattribute:actionnameselectorattribute{Public string Name {get; set;} Public Multibuttonattribute (string name) {this . name = name; } public override bool IsValidName (ControllerContext controllercontext, string actionname, System.Reflection.MethodInfo MethodInfo) { if (string. IsNullOrEmpty (this. Name) { return false; } Return ControllerContext.HttpContext.Request.Form.AllKeys.Contains (this. Name);} }
References: http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx
Method Four, improve
Thomas Eyde An improved version of method three:
Controller:
[HttpPost] [Multibutton (Name = "Delete", Argument = "id")] public ActionResult Delete (string id) { var response = System.Web.Http Context.Current.Response; Response. Write ("Delete action is invoked with" + ID);
View:
<input type= "Submit" value= "not important" name= "delete"/> <input type= "Submit" value= "not important" name= " Delete:id "/>
Multibuttonattribute definition:
[AttributeUsage (AttributeTargets.Method, AllowMultiple = False, inherited = True)] public class Multibuttonattribute: Actionnameselectorattribute {public string Name {get; set;} public string Argument {get; set;} public override bool IsValidName (ControllerContext controllercontext, String acti Onname, MethodInfo MethodInfo) {var key = Buttonkeyfrom (ControllerContext); var keyisvalid = IsValid (key); if (keyisvalid) {Updatevalueproviderin (ControllerContext, Valuefrom (key)); } return keyisvalid; } private String Buttonkeyfrom (ControllerContext controllercontext) {var keys = Controllercon Text. HttpContext.Request.Params.AllKeys; return keys. FirstOrDefault (Keystartswithbuttonname); } private static bool IsValid (string key) {return key ! = NULL; } private static string Valuefrom (String key) {var parts = key. Split (":". ToCharArray ()); return parts. Length < 2? NULL:PARTS[1]; } private void Updatevalueproviderin (ControllerContext controllercontext, String value) {if (string. IsNullOrEmpty (Argument)) return; Controllercontext.controller.valueprovider[argument] = new Valueproviderresult (value, value, NULL); } private bool Keystartswithbuttonname (string key) {return key. StartsWith (Name, stringcomparison.invariantcultureignorecase); }}//If it is in MVC 2.0, change the Updatevalueproviderin method to: private void Updatevalueproviderin (ControllerContext ControllerContext, String value) {if (string. IsNullOrEmpty (Argument)) return; Controllercontext.routedata.values[this. Argument] = value;}
Several ways to implement multiple button submissions in ASP.