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 ...
随机推荐
- 带卡扣的网卡接口使用小Tips,大家注意插拔网线的手法啊!
最近入手了一台X401,因为机器本身比较薄,它的网卡接口是有卡扣的,插网线的时候卡扣往下沉,这种设计应该有很多机型都采用了.但是大家有没有发现啊,这种接口的卡扣,时间长了,可能会有点松动.为了保护爱机 ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- 基于cocos2d-x的Android游戏中使用fmod音频引擎
cocos2d-x的音频引擎是cocosDenshion, 它的Android版比较弱, 只能播放一个背景音乐和些许音效, 如果要实现稍微复杂一点的音频播放, 比如同时播放几个音轨就不能了. 这一点远 ...
- TCP发送接口的返回值
原文链接: http://blog.csdn.net/ordeder/article/details/17240221 1. TCP发送接口:send() TCP发送数据的接口有send,write, ...
- .jre下的lib和jdk下的lib的区别
jre是JDK的一个子集.提供一个运行环境.JDK的lib目录是给JDK用的,例如JDK下有一些工具,可能要用该目录中的文件.例如,编译器等.JRE的lib目录是为JVM,运行时候用的.包括所有的标准 ...
- linux之ps命令
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- SQL数据库的十条命令
--(1)查询每个总学时数 select GradeId,SUM(classHour) from subject group by GradeId order by(SUM(classHour)) - ...
- MQTT服务器搭建-mosquitto1.4.4安装指南
Mosquitto mosquitto是一款实现了 MQTT v3.1 协议的开源的消息代理服务软件. 其提供了非常轻量级的消息数据传输协议,采用发布/订阅模式进行工作,可用于物联设备.中间件.APP ...
- 基于web的IM软件通信原理分析
关于IM(InstantMessaging)即时通信类软件(如微信,QQ),大多数都是桌面应用程序或者native应用较为流行,而网上关于原生IM或桌面IM软件类的通信原理介绍也较多,此处不再赘述.而 ...
- C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
1. 主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...