【MVC5】画面多按钮提交

画面上有个多个按钮时,如何绑定到各自的Action上?

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 项目笔记汇总

转载请标明原文出处!http://www.cnblogs.com/Ryukaka/
posted @   shiningrise  阅读(417)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2012-12-11 bind9搭建dns,本网可解析,其他网段只解析自定义的域名,不解析公网
2012-12-11 搭建视频网站光线cms
2012-12-11 Nginx 反向代理设置
2009-12-11 删除表数据并重置自动编号列TRUNCATE TABLE
2009-12-11 评教系统统计功能设想
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css
点击右上角即可分享
微信分享提示