• MVC页面上多个提交按钮提交到不同的Action


    使用mvc扩展类,ActionNameAttribute方法如下:

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
        public class MultiButtonAttribute : ActionNameSelectorAttribute
        {
            public string Name { get; set; }
            public string Argument { get; set; }
            public MultiButtonAttribute(string name)
            {
                this.Name = name;
            }
            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.RouteData.Values[this.Argument] = value;
            }
    
    
            private bool KeyStartsWithButtonName(string key)
            {
                return key.StartsWith(Name, StringComparison.InvariantCultureIgnoreCase);
            }
    
    
        }

    Controller:

    [HttpPost] 
    [MultiButton(Name = "delete", Argument = "id")] 
    public ActionResult Delete(string id) 
    { 
        return View(); 
    } 

    或者

            [HttpPost]
            [MultiButton("btnSearch")]
            public ActionResult Index(AttendanceList model)
            {
                    return View(model);
            }

    View:

    <input type="submit" value="查询"  name="btnSearch" />
    <input type="submit" value="删除" name="delete:id" />
  • 相关阅读:
    CentOS 下搭建Jenkins
    SRVE0255E: 尚未定义要处理 ***的 Web 组/虚拟主机。
    WebSphere Application Server中manageprofiles的使用
    WAS 与IHS集成问题
    CentOS ln 链接
    VIM常见命令
    虚拟机VM下CentOS7部署WASND9+HTTP9
    CentOS7下安装GUI图形界面
    CentOS 系统时间与硬件时间
    hive 排序和聚集
  • 原文地址:https://www.cnblogs.com/wuyubing/p/4898654.html
Copyright © 2020-2023  润新知