【问题标题】:Image submit button works with Chrome but dosn't work with firefox?图片提交按钮适用于 Chrome,但不适用于 Firefox?
【发布时间】:2013-06-14 07:41:38
【问题描述】:

我有以下 HTML:

<input type="image" src="/Images/actions/Delete.gif" alt="Delete" title="Delete" name="action" value="Delete"/>

在我的控制器中,我有以下自定义属性

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
    public string MatchFormKey { get; set; }
    public string MatchFormValue { get; set; }
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        return controllerContext.HttpContext.Request[MatchFormKey] != null &&
            controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
    }
}

单击删除时应调用以下操作方法

    [HttpPost]
    [MultiButton(MatchFormKey = "action", MatchFormValue = "Delete")]
    public ActionResult Delete(MessageModel model)
    {
        return Content("Delete clicked");
    }

这与 Chrome 完美配合,但是当在 Firefox 中单击提交按钮时,我的操作方法 Delete() 不会被调用。

任何想法我做错了什么?

【问题讨论】:

    标签: google-chrome firefox post asp.net-mvc-4 custom-attributes


    【解决方案1】:

    根据 HTML 规范,<input name="action" type="image"> 在单击时会发送名为 action.xaction.y 的表单参数,但不会发送名为 action 的表单参数。 Firefox 遵循规范,而 Chrome 没有。

    不过,您的服务器端代码似乎明确检查了 action,这就是它在 Firefox 中不起作用的原因。

    【讨论】:

      【解决方案2】:

      某些浏览器以不同的方式发送值。因此,您必须更深入地挖掘 RouteData,而不是使用普通请求,您将在其中找到所有值,包括来自 Firefox 的值。这是您的自定义属性的修改版本,适用于 Chrome、Firefox 和 IE。

      [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
      public class MultiButtonAttribute : ActionNameSelectorAttribute
      {
          public string MatchFormKey { get; set; }  
          public string MatchFormValue { get; set; } 
          public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
          {
              if (controllerContext.HttpContext.Request.RequestContext.RouteData.Values[MatchFormKey] != null)
              {
                  return (string)controllerContext.HttpContext.Request.RequestContext.RouteData.Values[MatchFormKey] == MatchFormValue;
              }
              return false;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-14
        • 1970-01-01
        • 2018-09-08
        • 2014-11-17
        • 2015-06-09
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多