MVC3实现多个按钮提交
有时我们需要在同一个Form表单中进行多个按钮的提交,来完成不同的功能。因为MVC中一个表单默认只能提交到一个Action处理,相对比较麻烦。
1、使用客户端脚本
我们可以view中加上如下代码:
<input type="submit" value="SubmitToAbout1" onclick='this.form.action="@Url.Action("About1")" '/> <br /> <input type="submit" value="SubmitToAbout2" onclick='this.form.action="@Url.Action("About2")" '/>
用来指示不同的按钮点击后跳转到不同的Action。Action方法代码如下
public string About1(FormCollection collection) { return collection["Title"]; } public string About2(FormCollection collection) { return collection["Title"]; }
这样就实现了点击不同的按钮跳转到不同的方法中。
2、在Action中判断按钮的value值
在view中,不同的input标签,定义相同的name,如下:
<input type="submit" value="SubmitToAbout1" name="action"/> <input type="submit" value="SubmitToAbout2" name="action"/>
然后在Action方法中,获取表单传过来的数据,获取name为action的value值,再根据value值去进行判断。
[HttpPost] public ActionResult About(FormCollection collection) { string action=collection["action"]; if (action == "SubmitToAbout1") { ViewBag.action = "SubmitToAbout1"; } else if (action == "SubmitToAbout2") { ViewBag.action = "SubmitToAbout2"; } return View(); }
此种方法我们可以做出改进,新建一个MultiButtonAttribute类,该类继承ActionNameSelectorAttribute类,该类代码如下:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class MultiButtonAttribute : ActionNameSelectorAttribute { public string MatchFormKey { get; set; } public string MatchFormValue { get; set; } public override bool IsValidName(ControllerContext controllerContext, string actionName, System.Reflection.MethodInfo methodInfo) { bool state= controllerContext.HttpContext.Request[MatchFormKey] != null && controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue; return state; } }
其中view中代码不变,还是
<input type="submit" value="SubmitToAbout1" name="action"/> <input type="submit" value="SubmitToAbout2" name="action"/>
在控制器的Action中调用:
[HttpPost] [MultiButton(MatchFormKey = "action", MatchFormValue = "SubmitToAbout1")] public string About1(FormCollection collection) { return "SubmitToAbout1 clicked"; } [HttpPost] [MultiButton(MatchFormKey = "action", MatchFormValue = "SubmitToAbout2")] public string About2(FormCollection collection) { return "SubmitToAbout2 clicked"; }
这样,点击submit按钮时,MultiButton会根据按钮的name和value值跳转到相应的执行方法。
3、判断按钮的name值
判断按钮的value值有很多不确定的因素,比如按钮的value值可能会更加客户的个人爱好而进行改动,或者按钮需要改成多语言版本的,这时我们根据按钮的value值来选择不同的控制器的Action时就显得极其不灵活,这时我们希望通过按钮的name值来选择其相应的Action方法。同样还是自定义一个MultiButtonAttribute类,该类继承ActionNameSelectorAttribute类,该类代码为:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class MultiButtonAttribute : ActionNameSelectorAttribute { public string MatchFormValue { get; set; } public override bool IsValidName(ControllerContext controllerContext, string actionName, System.Reflection.MethodInfo methodInfo) { bool state = !string.IsNullOrEmpty(MatchFormValue) && controllerContext.HttpContext.Request.Form.AllKeys.Contains(this.MatchFormValue); return state; } }
视图上按钮代码为:
<input type="submit" value="SubmitToAbout1" name="about1"/> <input type="submit" value="SubmitToAbout2" name="about2"/>
控制器Action代码:
[HttpPost] [MultiButton(MatchFormValue = "about1")] public string About1(FormCollection collection) { return "SubmitToAbout1 clicked"; } [HttpPost] [MultiButton(MatchFormValue = "about2")] public string About2(FormCollection collection) { return "SubmitToAbout2 clicked"; }