【MVC5】画面多按钮提交
2015-12-11 14:54
260 查看
画面上有个多个按钮时,如何绑定到各自的Action上?
1.追加如下MultipleButtonAttribute类
2.Razor(给按钮追加name属性,值的格式为【{Name}:{Argument}】)
3.Controller(参数Name和Argument对应按钮的name属性值)
原文地址:http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework
已添加到【MVC5】ASP.NET MVC 项目笔记汇总
1.追加如下MultipleButtonAttribute类
1 using System; 2 using System.Reflection; 3 using System.Web.Mvc; 4 5 namespace DailyReportSystem.Attribute 6 { 7 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 8 public class MultipleButtonAttribute : ActionNameSelectorAttribute 9 { 10 public string Name { get; set; } 11 public string Argument { get; set; } 12 13 public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) 14 { 15 var isValidName = false; 16 var keyValue = string.Format("{0}:{1}", Name, Argument); 17 var value = controllerContext.Controller.ValueProvider.GetValue(keyValue); 18 19 if (value != null) 20 { 21 controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument; 22 isValidName = true; 23 } 24 25 return isValidName; 26 } 27 } 28 }
2.Razor(给按钮追加name属性,值的格式为【{Name}:{Argument}】)
<form action="" method="post"> <input type="submit" value="Save" name="action:Save" /> <input type="submit" value="Cancel" name="action:Cancel" /> </form>
3.Controller(参数Name和Argument对应按钮的name属性值)
[HttpPost] [MultipleButton(Name = "action", Argument = "Save")] public ActionResult Save(MessageModel mm) { ... } [HttpPost] [MultipleButton(Name = "action", Argument = "Cancel")] public ActionResult Cancel(MessageModel mm) { ... }
原文地址:http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework
已添加到【MVC5】ASP.NET MVC 项目笔记汇总
相关文章推荐
- Javascript变长参数和默认参数
- 百度地图使用2-自定义覆盖物
- console.log()
- 创建Node Js应用
- Comparable和Comparator以及Arrays.sort方法
- Eclipse开发过程中个VM Arguments的设置
- 简单内存泄漏检测方法,解决Detected memory leaks!问题
- Java学习笔记----线程2
- inode
- ant入门学习
- SQL Server 2008功能选择
- 配置共享文件夹--不同文件夹设置不同访问权限
- IOS点击文字展开滚轮实现,类似下拉列表
- [Windows Server 2003] 还原SQL Server数据库
- dedecms 去掉栏目页的预览功能
- 平时常用sql语句集锦
- hbase filter使用
- 如何解决虚拟机Mac OS X 不支持二进制编译问题()
- 陈词滥调,正确使用memset
- css基础知识