mvc多个按钮的提交方法
转载地址:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。
如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。
方法一:使用客户端脚本
比如我们在View中这样写:
1
2
3
|
< input type="submit" value="审核通过" onclick='this.form.action="<%=Url.Action("Action1") %>";' /> < input type="submit" value="审核不通过" onclick='this.form.action="<%=Url.Action("Action2") %>";' /> < input type="submit" value="返回" onclick='this.form.action="<%=Url.Action("Action3") %>";' /> |
在点击提交按钮时,先改变Form的action属性,使表单提交到按钮相应的action处理。
但有的时候,可能Action1和2的逻辑非常类似,也许只是将某个字段的值置为1或者0,那么分开到二个action中又显得有点多余了。
方法二:在Action中判断通过哪个按钮提交
在View中,我们不用任何客户端脚本处理,给每个提交按钮加好name属性:
1
2
3
|
< input type="submit" value="审核通过" name="action" /> < input type="submit" value="审核不通过" name="action"/> < input type="submit" value="返回" name="action"/> |
然后在控制器中判断:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[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的选择。
使用此方法,我们可以将控制器写成这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[HttpPost] [MultiButton( "action1" )] public ActionResult Action1() { // return View(); } [HttpPost] [MultiButton( "action2" )] public ActionResult Action2() { // return View(); } |
在 View中:
1
2
3
|
< input type="submit" value="审核通过" name="action1" /> < input type="submit" value="审核不通过" name="action2"/> < input type="submit" value="返回" name="action3"/> |
此时,Controller已经无须依赖于按钮的Value值。
MultiButtonAttribute的定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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); } } |
方法四、改进
Thomas Eyde就方法三的方案给出了个改进版:
Controller:
1
2
3
4
5
6
7
8
|
[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:
1
2
|
< input type="submit" value="not important" name="delete" /> < input type="submit" value="not important" name="delete:id" /> |
MultiButtonAttribute定义:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
[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方法改为:
1
2
3
4
5
6
|
private void UpdateValueProviderIn(ControllerContext controllerContext, string value) { if ( string .IsNullOrEmpty(Argument)) return ; controllerContext.RouteData.Values[ this .Argument] = value; } |
mvc多个按钮的提交方法的更多相关文章
- 脚本解决.NET MVC按钮重复提交问题
见于:Avoiding Duplicate form submission in Asp.net MVC by clicking submit twice 脚本代码: $(document).on(' ...
- ASP.NET MVC 5 - 查询Details和Delete方法
在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...
- jsp中普通按钮如何提交表单
jsp中普通按钮如何提交表单方法1: <form action = "提交的地址"> <input type="submit" ...
- Spring MVC与表单日期提交的问题
Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...
- MVC 5 - 查询Details和Delete方法
MVC 5 - 查询Details和Delete方法 在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看De ...
- Asp.net MVC4高级编程学习笔记-模型学习第五课MVC表单和HTML辅助方法20171101
MVC表单和HTML辅助方法 一.表单的使用. 表单中的action与method特性.Action表示表单要提交往那里,因此这里就有一个URL.这个URL可以是相对或绝对地址.表单默认的method ...
- [转]ASP.NET MVC 5 - 查询Details和Delete方法
在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...
- jQuery实现form表单基于ajax无刷新提交方法详解
本文实例讲述了jQuery实现form表单基于ajax无刷新提交方法.分享给大家供大家参考,具体如下: 首先,新建Login.html页面: <!DOCTYPE html PUBLIC &quo ...
- AJAX提交方法(POST)Demon
AJAX的POST提交方法,本质上来看和GET差不多,有些细小的区别,POST要提交数据时,需要setRequestHeader()方法来提交HTTP头,然后send()方法中提交数据(格式为:&qu ...
随机推荐
- windows核心编程---第七章 用户模式下的线程同步
用户模式下的线程同步 系统中的线程必须访问系统资源,如堆.串口.文件.窗口以及其他资源.如果一个线程独占了对某个资源的访问,其他线程就无法完成工作.我们也必须限制线程在任何时刻都能访问任何资源.比如在 ...
- sql基本增删改查语法
sql语法学习(适合新手) 1.插入数据 语法格式: INSERT [INTO] <表名> [列名] VALUES <值列表> insert into students(sna ...
- HttpClient(4.3.3)实例讲解
HttpClient的作用强大,真的是十分强大. 本实例是基于v4.3.3写的,,作用是模拟登陆后进行上下班打卡,,,使用htmlparser进行解析返回的html文件 关于HttpClient的一些 ...
- 与NS2一起度过第一个圣诞夜!(NS2入门学习参考资料)
Merry xmas! 安装好NS2后正式开始学习NS2啦,先转发一哥们的博客内容,慢慢看! 一). NS常用基本网站 1. 寻求问题答案最好的地方. http:/ ...
- Devexpress datagrid动态添加显示指定列的gridView
代码如下: public class DXGridControlHelper { /// <summary> /// 获取显示指定列的GridView /// </summary&g ...
- 字体Unicode编码
客专家福利 有奖试读&征文——我们在互联网上奋斗的故事 10月推荐文章汇总 加入“技术热心人”,赢丰厚奖品 html-中文字体在CSS中的显示(Unicode编码) ...
- D - Half of and a Half 大数
D - Half of and a Half Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I ...
- C++ 迭代器模式实现
STL模板库中有大量迭代器实现,这些迭代器隔离了算法实现与访问接口,我们也可以编写属于自己的迭代器.STL中的迭代器均继承至一个通用迭代器接口: template <class _Categor ...
- HDU-2778 DNA Sequence(AC自动机)
题目大意:统计模式串出现的次数. 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queu ...
- IIS-反向代理
测试环境:Windows10.IIS/10.0 1.安装ARR.URL Rewrite(URL重写工具2.0) 注意英文和中文环境的对应: Application Request Routing 对应 ...