有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能。

如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较麻烦点。

方法一:使用客户端脚本

比如我们在View中这样写:

  1. <input type="submit" value="审核通过"  onclick='this.form.action="<%=Url.Action("Action1") %>";' />
  2. <input type="submit" value="审核不通过"  onclick='this.form.action="<%=Url.Action("Action2") %>";'  />
  3. <input type="submit" value="返回"   onclick='this.form.action="<%=Url.Action("Action3") %>";' />

在点击提交按钮时,先改变Form的action属性,使表单提交到按钮相应的action处理。

但有的时候,可能Action1和2的逻辑非常类似,也许只是将某个字段的值置为1或者0,那么分开到二个action中又显得有点多余了。

方法二:在Action中判断通过哪个按钮提交

在View中,我们不用任何客户端脚本处理,给每个提交按钮加好name属性:

  1. <input type="submit" value="审核通过" name="action" />
  2. <input type="submit" value="审核不通过"  name="action"/>
  3. <input type="submit" value="返回"  name="action"/>

然后在控制器中判断:

  1. [HttpPost]
  2. public ActionResult Index(string action /* 其它参数*/)
  3. {
  4. if (action=="审核通过")
  5. {
  6. //
  7. }
  8. else if (action=="审核不通过")
  9. {
  10. //
  11. }
  12. else
  13. {
  14. //
  15. }
  16. }

几年前写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. [HttpPost]
  2. [MultiButton("action1")]
  3. public ActionResult Action1()
  4. {
  5. //
  6. return View();
  7. }
  8. [HttpPost]
  9. [MultiButton("action2")]
  10. public ActionResult Action2()
  11. {
  12. //
  13. return View();
  14. }

在 View中:

  1. <input type="submit" value="审核通过" name="action1" />
  2. <input type="submit" value="审核不通过"  name="action2"/>
  3. <input type="submit" value="返回"  name="action3"/>

此时,Controller已经无须依赖于按钮的Value值。

MultiButtonAttribute的定义如下:

  1. public class MultiButtonAttribute : ActionNameSelectorAttribute
  2. {
  3. public string Name { get; set; }
  4. public MultiButtonAttribute(string name)
  5. {
  6. this.Name = name;
  7. }
  8. public override bool IsValidName(ControllerContext controllerContext,
  9. string actionName, System.Reflection.MethodInfo methodInfo)
  10. {
  11. if (string.IsNullOrEmpty(this.Name))
  12. {
  13. return false;
  14. }
  15. return controllerContext.HttpContext.Request.Form.AllKeys.Contains(this.Name);
  16. }
  17. }

参考:http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

方法四、改进

Thomas Eyde就方法三的方案给出了个改进版:

Controller:

  1. [HttpPost]
  2. [MultiButton(Name = "delete", Argument = "id")]
  3. public ActionResult Delete(string id)
  4. {
  5. var response = System.Web.HttpContext.Current.Response;
  6. response.Write("Delete action was invoked with " + id);
  7. return View();
  8. }
 
  1. <input type="submit" value="not important" name="delete" />
  2. <input type="submit" value="not important" name="delete:id" />

MultiButtonAttribute定义:

  1. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  2. public class MultiButtonAttribute : ActionNameSelectorAttribute
  3. {
  4. public string Name { get; set; }
  5. public string Argument { get; set; }
  6. public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
  7. {
  8. var key = ButtonKeyFrom(controllerContext);
  9. var keyIsValid = IsValid(key);
  10. if (keyIsValid)
  11. {
  12. UpdateValueProviderIn(controllerContext, ValueFrom(key));
  13. }
  14. return keyIsValid;
  15. }
  16. private string ButtonKeyFrom(ControllerContext controllerContext)
  17. {
  18. var keys = controllerContext.HttpContext.Request.Params.AllKeys;
  19. return keys.FirstOrDefault(KeyStartsWithButtonName);
  20. }
  21. private static bool IsValid(string key)
  22. {
  23. return key != null;
  24. }
  25. private static string ValueFrom(string key)
  26. {
  27. var parts = key.Split(":".ToCharArray());
  28. return parts.Length < 2 ? null : parts[1];
  29. }
  30. private void UpdateValueProviderIn(ControllerContext controllerContext, string value)
  31. {
  32. if (string.IsNullOrEmpty(Argument)) return;
  33. controllerContext.Controller.ValueProvider[Argument] = new ValueProviderResult(value, value, null);
  34. }
  35. private bool KeyStartsWithButtonName(string key)
  36. {
  37. return key.StartsWith(Name, StringComparison.InvariantCultureIgnoreCase);
  38. }
  39. }

如果是在MVC 2.0中的话,将UpdateValueProviderIn方法改为:

  1. private void UpdateValueProviderIn(ControllerContext controllerContext, string value)
  2. {
  3. if (string.IsNullOrEmpty(Argument))
  4. return;
  5. controllerContext.RouteData.Values[this.Argument] = value;
  6. }

转自:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html

转:MVC单表多按钮提交的更多相关文章

  1. ASP.NET MVC实现多个按钮提交事件

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  2. MVC中实现多按钮提交(转)

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  3. ASP.NET MVC 表单的几种提交方式

    下面是总结一下在ASP.NET MVC中表单的几种提交方式. 1.Ajax提交表单 需要引用 <script type="text/javascript" src=" ...

  4. spring mvc form表单提交乱码

    spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...

  5. Spring MVC与表单日期提交的问题

    Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...

  6. javaWeb中一个按钮提交两个表单

    一个按钮提交两个表单,有时候会用到,一般会很容易想到使用 onclick="document.form1.submit();document.form2.submit();" 的方 ...

  7. jQuery实现button按钮提交表单

    在JSP页面中,通常使用button按钮提交表单数据,使用jQuery实现代码如下: <span style="font-family:Comic Sans MS;font-size: ...

  8. 如何为Form表单的多个提交按钮指定不同的Action地址?

    这是我很久以前看到的一个技巧,但我忘记在哪里了,当时遇到这样的需求,做了笔记,现在整理成文章分享出来,因为我感觉这个小技巧还是挺有用的,这种应用场景也算比较常见,比如一个表单有"保存&quo ...

  9. 一个form表单,多个提交按钮

    技巧就是把提交的input的类型改成button!这样就可以实现多个按钮提交! 以下是案例: <form action="" id="tijiao"> ...

随机推荐

  1. 回顾Spring框架

    Spring框架: 传统JavaEE解决企业级应用问题时的"重量级"架构体系,使它的开发效率,开发难度和实际的性能都令人失望.Spring是以一个 救世主的身份降临在广大的程序员面 ...

  2. brute-force search

    #include <pcl/search/brute_force.h> #include <pcl/common/common.h> #include <iostream ...

  3. 服务器自己用户名下编译gcc

    要点: 1.上传gcc 学习命令 scp 具体格式: scp local_file remote_username@remote_ip:remote_folder scp /home/linux/so ...

  4. php部分--session的三种用法

    一.在不同页面之间显示用户的信息 二.控制登录 1.登录页面 <body> <form action="loginchuli.php" method=" ...

  5. Logistic回归原理及公式推导[转]

    原文见 http://blog.csdn.net/acdreamers/article/details/27365941 Logistic回归为概率型非线性回归模型,是研究二分类观察结果与一些影响因素 ...

  6. import logging 导入记录日志包

    import logging 日志几个级别 logging.debug logging.info logging.error

  7. js监听rem实现响应式

    原文链接:http://caibaojian.com/web-app-rem.html (function (doc, win) { var docEl = doc.documentElement, ...

  8. 【netty】Netty系列之Netty百万级推送服务设计要点

    1. 背景 1.1. 话题来源 最近很多从事移动互联网和物联网开发的同学给我发邮件或者微博私信我,咨询推送服务相关的问题.问题五花八门,在帮助大家答疑解惑的过程中,我也对问题进行了总结,大概可以归纳为 ...

  9. threadid=1: thread exiting with uncaught.exception ......解决方法

     threadid=1: thread exiting with uncaught exception (group=0x40015560)E/AndroidRuntime(285): FATAL E ...

  10. 少见的sql

    1,values 的新用法,出现自2008 SELECT * FROM table AS a ,,,'qq3')) tem(id,name) ON a.id=tem.id insert into xx ...