Asp. NET Tutorial: web page form multiple buttons to complete different functions

Source: Internet
Author: User
Tags allkeys definition bool functions httpcontext net string version

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: Use client script

For example, we write this in view:

The following are the referenced contents:

<input type= "Submit" value= "Audit passed"/>

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: Determine by which button to submit in action

In view, we do not use any client script to add the name attribute to each submit button:

The following are the referenced contents:

<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:

The following are the referenced contents:

[HttpPost]
Public ActionResult Index (String action/* Other parameters */)
{
if (action== "Audit passed")
{
//
}
else if (action== "Audit does 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.

Reference: http://www.ervinter.com/2009/09/25/asp-net-mvc-how-to-have-multiple-submit-button-in-form/

Method Three: Use 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:

The following are the referenced contents:

[HttpPost]
[Multibutton ("Action1")]
Public ActionResult Action1 ()
{
//
return View ();
}
[HttpPost]
[Multibutton ("Action2")]
Public ActionResult Action2 ()
{
//
return View ();
}

In view:

The following are the referenced contents:

<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, controller no longer has to rely on the value of the button.

The definition of Multibuttonattribute is as follows:

The following are the referenced contents:

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);
}
}

Reference: Http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

Methods four, improve

Thomas Eyde An improved version of method three:

Controller:

The following are the referenced contents:

[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:

The following are the referenced contents:

<input type= "Submit" value= "not important" name= "delete"/>
<input type= "Submit" value= "not important" name= "Delete:id"/>

Multibuttonattribute definition:

The following are the referenced contents:

[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:

The following are the referenced contents:

private void Updatevalueproviderin (ControllerContext controllercontext, String value)
{
if (string. IsNullOrEmpty (Argument))
Return
Controllercontext.routedata.values[this. Argument] = value;
}

Original address:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html



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.