There are times when you need multiple buttons on a form to perform different functions, such as a simple approval function.
If you're using WebForm it doesn't need to be discussed, but a form in asp.net MVC can only be submitted to one action, which is relatively troublesome.
method One: using client Script
For example, we write this in view:
<inputtype= "Submit" value= "Audit passed" 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 to make the form submit to the button's corresponding action.
Sometimes, however, Action1 is very similar to the logic of 2, and perhaps just putting a field's value at 1 or 0, then separating it into two action is a bit redundant.
Method Two: deciding which button to submit through the action
In view, we do not use any client script to add the name attribute to each submit 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"/>
And then judge in the controller:
[HttpPost]
Public ActionResult Index (String action/* Other parameters *
/) {
if (action== "Audit Pass")
{
//
}
else if ( action== "Audit not pass")
{
//
}
else
{
//
}
}
Write ASP code A few years ago often use this method ...
The view becomes simple and controller complex.
Too dependent on the view, there are some problems. If one day the customer said that the text on the button was changed to "Pass the audit", or to do a multilingual version, that would be troublesome.
Method Three: using Actionselector
The basic principle of actionselector can be seen in this post using the Actionselector control action option.
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 not through" name= "Action2"/ >
<input type= "Submit" value= "return" name= "Action3"/>
At this point, controller no longer has 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);
}
method Four: Improved
Controller:
[HttpPost]
[Multibutton (Name = "Delete", Argument = "id")]
Public ActionResult Delete (string id)
{
var response = System.Web.HttpContext.Current.Response;
Response. Write ("Delete action is invoked with" + ID);
return View ();
View:
<input type= "Submit" value= "not Important" "name=" delete "/> <input type=
" Submit "value=" not important " Name= "Delete:id"/>
Multibuttonattribute definition:
Code
[AttributeUsage (AttributeTargets.Method, AllowMultiple = False, inherited = True)] public class Multibuttonattribute:a
Ctionnameselectorattribute {public string Name {get; set;}
public string Argument {get; set;} public override bool IsValidName (ControllerContext controllercontext, String actionname, MethodInfo MethodInfo) {var k
EY = Buttonkeyfrom (ControllerContext);
var keyisvalid = IsValid (key);
if (keyisvalid) {Updatevalueproviderin (ControllerContext, Valuefrom (key));
return keyisvalid; private String Buttonkeyfrom (ControllerContext controllercontext) {var keys = ControllerContext.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 controllerconte XT, String value} {if (string).
IsNullOrEmpty (Argument)) return; Controllercontext.routedata.values[this.
Argument] = value;
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.