English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Sometimes, you may encounter a situation where multiple buttons are needed on a form to complete different functions, such as a simple approval function.
If it is a webform, there is no need to discuss it, but in ASP.NET MVC, a form can only submit to one Action handling, which is relatively麻烦.
Method One:Using client-side script
For example, we can write like this in the View:
<input type="submit" value="Passed Review" onclick='this.form.action="<%=Url.Action("Action1)%>"/> <input type="submit" value="Not Passed Review" onclick='this.form.action="<%=Url.Action("Action2)%>" /> <input type="submit" value="Return" onclick='this.form.action="<%=Url.Action("Action3)%>" />
When clicking the submit button, first change the action attribute of the Form, so that the form is submitted to the action handling corresponding to the button.
But sometimes, it may be that the Action1and2The logic is very similar, maybe just setting the value of a certain field to1Or 0, it seems a bit redundant to split them into two actions.
Method Two:Judge which button was submitted in the Action
In the View, we do not use any client-side script processing, and add the name attribute to each submit button:
<input type="submit" value="Passed Review" name="action" /> <input type="submit" value="Not Passed Review" name="action"/> <input type="submit" value="Return" name="action"/>
Then judge in the controller:
[HttpPost] public ActionResult Index(string action /* Other parameters*/) { if (action=="Passed Review") { // } else if (action=="Not Passed Review") { // } else { // } }
A few years ago, when writing ASP code, I often used such methods...
View becomes simple, while Controller becomes complex.
Too dependent on View may lead to some issues. If one day the customer says the text on the button should be changed to 'Passed Review', or a multilingual version is needed, it will be麻烦.
Method Three:Using ActionSelector
The basic principle of ActionSelector can be understood by looking at this POST that uses ActionSelector to control the selection of actions.
Using this method, we can write the controller like this:
[HttpPost] [MultiButton("action"1) public ActionResult Action1() { // return View(); } [HttpPost] [MultiButton("action"2) public ActionResult Action2() { // return View(); }
In View:
<input type="submit" value="Approved" name="action"1" /> <input type="submit" value="Not approved" name="action"2"/> <input type="submit" value="Return" name="action"3"/>
At this point, the Controller no longer depends on the button's Value value.
Definition of MultiButtonAttribute:
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:Improvement
Controller:
[HttpPost] [MultiButton(Name = "delete", Argument = "id")] public ActionResult Delete(string id) { var response = System.Web.HttpContext.Current.Response; response.Write("Delete action was 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 : ActionNameSelectorAttribute { public string Name { get; set; } public string Argument { get; set; } public override bool IsValidName(ControllerContext controllerContext, string actionName, 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 = 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 2In .0, change the UpdateValueProviderIn method to: private void UpdateValueProviderIn(ControllerContext controllerContext, string value) { if (string.IsNullOrEmpty(Argument)) return; controllerContext.RouteData.Values[this.Argument] = value; }
That's all for this article. I hope it will be helpful to everyone's learning and also hope everyone will support the Naying Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)