MVC中实现多按钮提交(转)
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。
![]()
如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。
方法一:使用客户端脚本
比如我们在View中这样写:
- <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") %>";' />
<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属性:
- <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);
}
}
方法四、改进
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
MVC中实现多按钮提交(转)的更多相关文章
- 转:MVC单表多按钮提交
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...
- ASP.NET MVC实现多个按钮提交事件
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...
- MVC中处理表单提交的方式(Ajax+Jquery)
MVC中处理表单有很多种方法,这里说到第一种方式:Ajax+Jquery 先看下表单: <form class="row form-body form-horizontal m-t&q ...
- mvc中form表单提交的几种形式
第一种方式:submit 按钮 提交 <form action="MyDemand" method="post"> <span>关键字: ...
- MVC中获取所有按钮,并绑定事件!
<script> var btns = $('[id=addbtn]'); //不能直接使用#ID来获取,必须用[] //循环遍历所有的按钮,一个一个添加事件绑定 for (var i ...
- ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法
MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交
- ASP.NET MVC中在Action获取提交的表单数据方法
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- 在ASP.NET MVC中使用UEditor无法提交的解决办法
很简单的一个ajax提交,却怎么都不成功 $.ajax({ type: "POST", url: "/mms/riskmanage/commitreply", ...
随机推荐
- C++ - 内置类型的最大值宏定义
内置类型的最大值宏定义 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24311895 C++中, 常常会使用, 某些类型的最大值 ...
- UVa 11587 - Brick Game
称号:背景:brick game有N块,给你一个整数的定数S,两个人轮流木: 的木块数是集合S中存在的随意数字.取走最后木块的人获胜.无法取则对方获胜. 题干:如今让你先取,给你一个你的结果序列串T, ...
- vs2015基于VisualStudioOnline协同工作流程
项目负责人登陆自己的vsonline新建项目就不多说了. 直接从邀请队友开始 项目负责人操作 被邀请的邮箱务必是可以登录visualstudio的邮箱 发送邀请后,被邀请人登陆自己的邮箱,查看邀请人发 ...
- 使用AppCompat_v7 21.0.0d的几个兼容问题
1.实现新的ActionBarDrawerToggle动画 ActionBarDrawerToggle使用最新的AppCompat_v7 21会出现一个非常帅的动画.使用方式在Androidstudi ...
- Android手机定位技术的发展
基于以下三种方式的移动位置:1. 网络位置 :2. 基站定位. 3. GPS定位 1 网络位置 前提是连接到网络:Wifi.3G.2G 到达IP址 比如:彩虹版QQ,珊瑚虫版QQ,就有一个功能显示对 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- HDU 5059 Help him(细节)
HDU 5059 Help him 题目链接 直接用字符串去比較就可以,先推断原数字正确不对,然后写一个推断函数,注意细节,然后注意判掉空串情况 代码: #include <cstdio> ...
- 命令模式 & 策略模式 & 模板方法
一.策略模式 策略模式:封装易变化的算法,可互相替换. GoF<设计模式>中说道:定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换.该模式使得算法可独立于它们的客户变化. 比如 ...
- Android适配方案小结(一)
相关计量单位介绍: px:是屏幕的像素点,不同设备显示的效果一样. in:英寸(1英寸等于2.54cm) mm:毫米 pt:磅, 1/72英寸 dp:device independent pixels ...
- [ExtJS5学习笔记]第22 Extjs5正在使用beforeLabelTpl添加所需的配置选项标注星号标记
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39395753 官方样例:http://docs.sencha.com/extjs/5. ...