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 ...
随机推荐
- mysql 日期类型比较
MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围 ------------ ------ ...
- High Precision Timers in iOS / OS X
High Precision Timers in iOS / OS X The note will cover the do's and dont's of using high precision ...
- 使用JavaScript设置、获取父子页面中的值
一:获取父页面中的值 有二种方法windows.open()和windows.showModalDialog() 1.windos.open(URL,name,reatures,replace) 再父 ...
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- java端口扫描(原创)
项目需要扫描占用的端口来判断服务是否启动,通过查资料发现大多数方法都是ServerSocket socket = new ServerSocket(port);代码如下: package com.fr ...
- LEETCODE —— Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- YOLO: Real-Time Object Detection 安装和测试
1.下载darknet git clone https://github.com/pjreddie/darknet.git 2.修改make GPU= CUDNN= OPENCV= DEBUG= 3. ...
- SQL Server使用convert对datetime日期数据进行获取
来源:http://database.51cto.com/art/201007/211883.htm 备注:本文的语法讲解确实是比较乱,似乎格式不太严谨.参考时还是以实例验证为准比较好 以下的文章主要 ...
- Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
#!/usr/bin/python# -*- coding: utf-8 -*- 解决方法: 可以看到我的版本是2.6的,所以打开/usr/lib64/python2.6/site.py 红框里本来是 ...
- Spring使用环境变量控制配置文件加载
项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...