【问题标题】:2 submit buttons on same form with MVC 3. Only one submit button is working in IE 92 个提交按钮在与 MVC 3 相同的表单上。只有一个提交按钮在 IE 9 中工作
【发布时间】:2014-10-08 09:28:30
【问题描述】:

我为提交按钮命名,然后在控制器方法中检查提交的值。当我单击注册按钮时,它不会触发服务器上的操作方法。这适用于 chrome 和 IE 10 及更高版本,但不适用于 IE9。

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="PostValue" value="UploadImage" />
<input type="submit" name="PostValue" value="Register" />
<% Html.EndForm(); %>

【问题讨论】:

    标签: html asp.net-mvc asp.net-mvc-3 internet-explorer-9


    【解决方案1】:

    我没有这方面的经验。 但是如果你不想使用同名的提交按钮,这个问题有一个解决方案:

    @using (Html.BeginForm("PostNews", "Home", FormMethod.Post))
    {
        <input type="submit" name="submit.Publish" value="Publish News" />
        <input type="submit" name="submit.Save" value="Save News" />
    }
    
    public class HomeController : Controller
    {
        [HttpPost, ActionName("PostNews"), ActionRequired("submit.Publish")]
        public ActionResult PublishNews()
        {
            ViewBag.Message = "Your app description page.";
    
            return View(...);
        }
    
        [HttpPost, ActionName("PostNews"), ActionRequired("submit.Save")]
        public ActionResult SaveNews()
        {
            return View(...); 
        }
    }
    
    public class ActionRequiredAttribute : ActionMethodSelectorAttribute
    {
        private readonly string _submitButtonName;
        public ActionRequiredAttribute(string submitButtonName)
        {
            _submitButtonName = submitButtonName;
        }
        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            var actionRequired = controllerContext.RequestContext.HttpContext.Request.Form[_submitButtonName];
            return !string.IsNullOrEmpty(actionRequired);
        }
    }
    

    【讨论】:

    • 感谢您的回复。但就我而言,我不能使用 2 actionresult 方法。我必须对两个按钮使用相同的操作。
    猜你喜欢
    • 2014-01-31
    • 2011-06-23
    • 2020-04-09
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    相关资源
    最近更新 更多