原创|其它|编辑:郝浩|2010-01-29 09:53:14.000|阅读 942 次
概述:有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。
如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。
比如我们在View中这样写:
1 |
< input type = "submit" value = "审核通过" onclick=' this.form.action = "<%=Url.Action(" Action1") %>";' /> |
2 |
< input type = "submit" value = "审核不通过" onclick=' this.form.action = "<%=Url.Action(" Action2") %>";' /> |
3 |
< input type = "submit" value = "返回" onclick=' this.form.action = "<%=Url.Action(" Action3") %>";' /> |
在点击提交按钮时,先改变Form的action属性,使表单提交到按钮相应的action处理。
但有的时候,可能Action1和2的逻辑非常类似,也许只是将某个字段的值置为1或者0,那么分开到二个action中又显得有点多余了。
在View中,我们不用任何客户端脚本处理,给每个提交按钮加好name属性:
1 |
< input type = "submit" value = "审核通过" name = "action" /> |
2 |
< input type = "submit" value = "审核不通过" name = "action" /> |
3 |
< input type = "submit" value = "返回" name = "action" /> |
然后在控制器中判断:
01 |
[HttpPost] |
02 |
public ActionResult Index( string action /* 其它参数*/ ) |
03 |
{ |
04 |
if (action== "审核通过" ) |
05 |
{ |
06 |
// |
07 |
} |
08 |
else if (action== "审核不通过" ) |
09 |
{ |
10 |
// |
11 |
} |
12 |
else |
13 |
{ |
14 |
// |
15 |
} |
16 |
} |
几年前写asp代码的时候经常用这样的方法…
View变得简单的,Controller复杂了。
太依赖说View,会存在一些问题。假若哪天客户说按钮上的文字改为“通过审核”,或者是做个多语言版的,那就麻烦了。
参考:http://www.ervinter.com/2009/09/25/asp-net-mvc-how-to-have-multiple-submit-button-in-form/
关于ActionSelector的基本原理可以先看下这个POST使用ActionSelector控制Action的选择。
使用此方法,我们可以将控制器写成这样:
01 |
[HttpPost] |
02 |
[MultiButton( "action1" )] |
03 |
public ActionResult Action1() |
04 |
{ |
05 |
// |
06 |
return View(); |
07 |
} |
08 |
[HttpPost] |
09 |
[MultiButton( "action2" )] |
10 |
public ActionResult Action2() |
11 |
{ |
12 |
// |
13 |
return View(); |
14 |
} |
在 View中:
1 |
< input type = "submit" value = "审核通过" name = "action1" /> |
2 |
< input type = "submit" value = "审核不通过" name = "action2" /> |
3 |
< input type = "submit" value = "返回" name = "action3" /> |
此时,Controller已经无须依赖于按钮的Value值。
MultiButtonAttribute的定义如下:
01 |
public class MultiButtonAttribute : ActionNameSelectorAttribute |
02 |
{ |
03 |
public string Name { get ; set ; } |
04 |
public MultiButtonAttribute( string name) |
05 |
{ |
06 |
this .Name = name; |
07 |
} |
08 |
public override bool IsValidName(ControllerContext controllerContext, |
09 |
string actionName, System.Reflection.MethodInfo methodInfo) |
10 |
{ |
11 |
if ( string .IsNullOrEmpty( this .Name)) |
12 |
{ |
13 |
return false ; |
14 |
} |
15 |
return controllerContext.HttpContext.Request.Form.AllKeys.Contains( this .Name); |
16 |
} |
17 |
} |
Thomas Eyde就方法三的方案给出了个改进版:
Controller:
1 |
[HttpPost] |
2 |
[MultiButton(Name = "delete" , Argument = "id" )] |
3 |
public ActionResult Delete( string id) |
4 |
{ |
5 |
var response = System.Web.HttpContext.Current.Response; |
6 |
response.Write( "Delete action was invoked with " + id); |
7 |
return View(); |
8 |
} |
View:
1 |
< input type = "submit" value = "not important" name = "delete" /> |
2 |
< input type = "submit" value = "not important" name = "delete:id" /> |
MultiButtonAttribute定义:
01 |
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false , Inherited = true )] |
02 |
public class MultiButtonAttribute : ActionNameSelectorAttribute |
03 |
{ |
04 |
public string Name { get ; set ; } |
05 |
public string Argument { get ; set ; } |
06 |
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) |
07 |
{ |
08 |
var key = ButtonKeyFrom(controllerContext); |
09 |
var keyIsValid = IsValid(key); |
10 |
if (keyIsValid) |
11 |
{ |
12 |
UpdateValueProviderIn(controllerContext, ValueFrom(key)); |
13 |
} |
14 |
return keyIsValid; |
15 |
} |
16 |
private string ButtonKeyFrom(ControllerContext controllerContext) |
17 |
{ |
18 |
var keys = controllerContext.HttpContext.Request.Params.AllKeys; |
19 |
return keys.FirstOrDefault(KeyStartsWithButtonName); |
20 |
} |
21 |
private static bool IsValid( string key) |
22 |
{ |
23 |
return key != null ; |
24 |
} |
25 |
private static string ValueFrom( string key) |
26 |
{ |
27 |
var parts = key.Split( ":" .ToCharArray()); |
28 |
return parts.Length < 2 ? null : parts[1]; |
29 |
} |
30 |
private void UpdateValueProviderIn(ControllerContext controllerContext, string value) |
31 |
{ |
32 |
if ( string .IsNullOrEmpty(Argument)) return ; |
33 |
controllerContext.Controller.ValueProvider[Argument] = new ValueProviderResult(value, value, null ); |
34 |
} |
35 |
private bool KeyStartsWithButtonName( string key) |
36 |
{ |
37 |
return key.StartsWith(Name, StringComparison.InvariantCultureIgnoreCase); |
38 |
} |
39 |
} |
如果是在MVC 2.0中的话,将UpdateValueProviderIn方法改为:
1 |
private void UpdateValueProviderIn(ControllerContext controllerContext, string value) |
2 |
{ |
3 |
if ( string .IsNullOrEmpty(Argument)) |
4 |
return ; |
5 |
controllerContext.RouteData.Values[ this .Argument] = value; |
6 |
} |
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:博客园