MVC中实现多按钮提交(转)
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。
如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。
方法一:使用客户端脚本
比如我们在View中这样写:
- <input type="submit" value="审核通过" οnclick='this.form.action="<%=Url.Action("Action1") %>";' />
- <input type="submit" value="审核不通过" οnclick='this.form.action="<%=Url.Action("Action2") %>";' />
- <input type="submit" value="返回" οnclick='this.form.action="<%=Url.Action("Action3") %>";' />
<input type="submit" value="审核通过" οnclick='this.form.action="<%=Url.Action("Action1") %>";' /> <input type="submit" value="审核不通过" οnclick='this.form.action="<%=Url.Action("Action2") %>";' /> <input type="submit" value="返回" οnclick='this.form.action="<%=Url.Action("Action3") %>";' />
在点击提交按钮时,先改变Form的action属性,使表单提交到按钮相应的action处理。
但有的时候,可能Action1和2的逻辑非常类似,也许只是将某个字段的值置为1或者0,那么分开到二个action中又显得有点多余了。
方法二:在Action中判断通过哪个按钮提交
在View中,我们不用任何客户端脚本处理,给每个提交按钮加好name属性:
- <input type="submit" value="审核通过" name="action" />
- <input type="submit" value="审核不通过" name="action"/>
- <input type="submit" value="返回" name="action"/>
<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
- {
- //
- }
- }
[HttpPost] public ActionResult Index(string action /* 其它参数*/) { if (action=="审核通过") { // } else if (action=="审核不通过") { // } else { // } }
几年前写asp代码的时候经常用这样的方法…
View变得简单的,Controller复杂了。
太依赖说View,会存在一些问题。假若哪天客户说按钮上的文字改为“通过审核”,或者是做个多语言版的,那就麻烦了。
参考: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();
- }
[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"/>
<input type="submit" value="审核通过" name="action1" /> <input type="submit" value="审核不通过" name="action2"/> <input type="submit" value="返回" name="action3"/>
此时,Controller已经无须依赖于按钮的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);
- }
- }
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();
- }
[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(); }
- <input type="submit" value="not important" name="delete" />
- <input type="submit" value="not important" name="delete:id" />
<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);
- }
- }
[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;
- }
private void UpdateValueProviderIn(ControllerContext controllerContext, string value) { if (string.IsNullOrEmpty(Argument)) return; controllerContext.RouteData.Values[this.Argument] = value; }
转自:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html
转载于:https://www.cnblogs.com/itjeff/p/3974870.html
MVC中实现多按钮提交(转)相关推荐
- Asp.net MVC中防止HttpPost重复提交
重复提交的场景很常见,可能是当时服务器延迟的原因,如购物车物品叠加,重复提交多个订单.常见的解决方法是提交后把Button在客户端Js禁用,或是用Js禁止后退键等.在ASP.NET MVC 3 Web ...
- ASP.NET MVC中在Action获取提交的表单数据方法总结
有Index视图如下: 视图代码如下: [html] view plaincopy <%@ Page Language="C#" MasterPageFile="~ ...
- ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)
有Index视图如下: 视图代码如下: [html] view plaincopyprint? <%@ Page Language="C#" MasterPageFile=& ...
- MVC中 关于退出按钮的写法
public ActionResult Logoff() { Session.Abandon(); Session.Clear(); FormsAuthentication.SignOut(); re ...
- MVC中一个表单实现多个提交按钮(一个action搞定添删改)
默认情况下,一个表单只能有一个提交事件,这不难理解,但有时我们需要有多个实现,早在ASP时代就可以实现这个功能,难道到了MVC时代后,功能削减了?当然不是,这和MVC本身有关,微软在这方面比较提倡一个 ...
- ASP.NET MVC中实现多个按钮提交的几种方法
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...
- mvc ajax提交多选,javascript – 如何使用Jquery AJAX调用MVC Action然后在MVC中提交表单?...
在我的MVC视图我有按钮: 当我点击这个按钮我需要调用一个动作,做一些东西,然后提交我的表单. 我有这个jQuery: $('#btnSave').click(function () { $.ajax ...
- php的mvc中的ajax请求参数为空,AJAX_asp.net中mvc使用ajax提交参数的匹配问题解决探讨,想到在asp.net的mvc中如果使用aja - phpStudy...
asp.net中mvc使用ajax提交参数的匹配问题解决探讨 想到在asp.net的mvc中如果使用ajax向服务端传递参数时如果参数是一个类或者是个数组(或List集合)以及更复杂的对象时,服务端总 ...
- 如何在ASP.NET MVC中实现提交若干个某模型的数据(某Model的List或ICollection,大小不定)
背景说明 在ASP.NET MVC中,有一个我们经常使用且十分好用的功能--模型绑定. 即在页面中指定该页面将会使用到的数据模型Model,然后在"显示数据"或"提交数据 ...
最新文章
- txt或者csv数据文件的格式是有要求的,如下shell代码中说明。
- Oracle 10G 全新创建数据库过程(暂记)
- java jsch 密钥登陆_我们可以使用JSch进行基于SSH密钥的通信吗?
- 云漫圈 | 敢怼我们程序员?哼,有你好看的。。。
- java 拦截html请求参数值_javaweb项目,html文件放在了WebRoot下,如何拦截访问html的请求呀?...
- Python数据可视化 Pyecharts 制作 Map3D 三维地图
- 今天2006的第一天,灰蒙蒙的元旦节!
- 我的谷歌变成了金色传说!
- 炒股软件周期分析怎么调,股票周期设置怎么设置
- C语言版动态通讯录(增、删、改、查(查重)、去重、排序、导出)
- ElasticSearch(搜索服务器)-第一天
- linux qq 提示错误,linux QQ使用实践兼wine QQ2013版本太低无法使用解决办法
- ZZULIOJ(C语言)1115数组最小值
- 一文搞懂内存映射原理及使用方法
- php文件直链源码,PHP萌心上传直链外链网盘源码
- 寻仙服务器维护公告,《古剑奇谭网络版》8月23日更新维护公告
- There are test failures.
- 虚拟内存的最大容量与实际容量区别
- wls matlab code,【 MATLAB】 Two-step WLS algorithm Simulation of TOA - Based Positioning
- 正则表达式:密码必须至少包含八个字符、至少包含一个数字、小写和大写的字母以及特殊字符...
热门文章
- debian/ubuntu 安装和使用perf
- Open vSwitch(OVS)版本与Linux内核的关系
- 五笔字根表识别码图_精选五笔输入法拆字原则,值得收藏
- php400错误的请求,Wordpress中的Ajax返回400错误请求,但不确定如何进一步调试 - WordPress - srcmini...
- linux 同步方法剖析,Linux 同步方法剖析
- 【每日一题】212. 单词搜索 II
- mysql分表搜索引擎_MySql分库分表总结(转)
- java log4j 文件_java log4j 日志文件
- java 图片合成 工具类_Java实现的图片上传工具类完整实例
- mysql变量使用总结