您的位置:首页 > 编程语言 > ASP

ASP.NET MVC中实现多个button提交的几种方法

2015-01-21 20:41 344 查看
有时候会遇到这样的情况:在一个表单上须要多个button来完毕不同的功能,比方一个简单的审批功能。



假设是用webform那不须要讨论,但asp.net mvc中一个表单仅仅能提交到一个Action处理,相对照较麻烦点。

方法一:使用client脚本

比方我们在View中这样写:

<inputtype="submit"value="审核通过"onclick='this.form.action="<%=Url.Action("Action1")%>/>
<inputtype="submit"value="审核不通过"onclick='this.form.action="<%=Url.Action("Action2")%>  />
<inputtype="submit"value="返回"onclick='this.form.action="<%=Url.Action("Action3")%>" />


在点击提交button时,先改变Form的action属性,使表单提交到button对应的action处理。

但有的时候,可能Action1和2的逻辑很类似,或许仅仅是将某个字段的值置为1或者0,那么分开到二个action中又显得有点多余了。

方法二:在Action中推断通过哪个button提交

在View中,我们不用不论什么client脚本处理,给每一个提交button加好name属性:

<input type="submit" value="审核通过" name="action" />
<input type="submit" value="审核不通过"  name="action"/>
<input type="submit" value="返回"  name="action"/>


然后在控制器中推断:

[HttpPost]
public ActionResult Index(string action /* 其他參数*/)
{
if (action=="审核通过")
{
//
}
else if (action=="审核不通过")
{
//
}
else
{
//
}
}


几年前写asp代码的时候经经常使用这个方案…

View变得简单的,Controller复杂了。

太依赖说View,会存在一些问题。假若哪天客户说button上的文字改为“通过审核”,或者是做个多语言版的,那就麻烦了。

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

方法三:使用ActionSelector

关于ActionSelector的基本原理能够先看下这个POST使用ActionSelector控制Action的选择

使用此方法,我们能够将控制器写成这样:

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


在 View中:

<input type="submit" value="审核通过" name="action1" />
<input type="submit" value="审核不通过"  name="action2"/>
<input type="submit" value="返回"  name="action3"/>


此时,Controller已经无须依赖于button的Value值。

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


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

方法四、改进

Thomas Eyde就方法三的方案给出了个改进版:

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

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

//假设是在MVC 2.0中的话,将UpdateValueProviderIn方法改为:

private void UpdateValueProviderIn(ControllerContext controllerContext, string value)
{
if (string.IsNullOrEmpty(Argument))
return;
controllerContext.RouteData.Values[this.Argument] = value;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: