ASP. net mvc implements multiple button submission methods, asp. netmvc

Source: Internet
Author: User
Tags allkeys

ASP. net mvc implements multiple button submission methods, asp. netmvc

Sometimes this problem occurs: Multiple buttons are required on a form to complete different functions, such as a simple approval function.

 

If webform is used, it does not need to be discussed. However, a form in asp.net mvc can only be submitted to one Action for processing, which is relatively troublesome.

Method 1:Use client scripts 

For example, we write in View as follows:

<Inputtype = "submit" value = "approved" onclick = 'this. form. action = "<% = Url. action ("Action1") %>/> <inputtype = "submit" value = "review failed" onclick = 'this. form. action = "<% = Url. action ("Action2") %>/> <inputtype = "submit" value = "return" onclick = 'this. form. action = "<% = Url. action ("Action3") %> "/>

When you click the submit button, first change the Form action attribute so that the Form is submitted to the corresponding action of the button for processing.

However, sometimes the logic of Action1 and Action2 is very similar. Maybe it is unnecessary to separate two actions by setting the value of a field to 1 or 0.

Method 2:Determine which button to submit in Action 

In the View, we do not need to process any client script, and add the name attribute to each submit button:

<Input type = "submit" value = "approved" name = "action"/> <input type = "submit" value = "not approved" name = "action "/> <input type = "submit" value = "return" name = "action"/>

Then judge in the controller:

[HttpPost] public ActionResult Index (string action/* Other parameters */) {if (action = "approved ") {//} else if (action = "review failed") {//} else {//}}

This method is often used when I write asp code a few years ago...

The View becomes simple and the Controller is complicated.

View is too dependent, which may cause some problems. If one day the customer says that the text on the button is changed to "approved" or a multi-language version, it will be troublesome.

Method 3:Use ActionSelector 

For the basic principle of ActionSelector, You can first take a look at this POST using ActionSelector to control the choice of Action.

Using this method, we can write the Controller as follows:

[HttpPost][MultiButton("action1")]public ActionResult Action1(){ // return View();}[HttpPost][MultiButton("action2")]public ActionResult Action2(){ // return View();}

In View:

<Input type = "submit" value = "approved" name = "action1"/> <input type = "submit" value = "REJECTED" name = "action2 "/> <input type = "submit" value = "return" name = "action3"/>

At this time, the Controller does not need to depend on the Value of the button.

MultiButtonAttribute is defined 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 4: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 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 ;}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.