【问题标题】:MVC ActionLink renders wrong html?MVC ActionLink 呈现错误的 html?
【发布时间】:2015-07-20 07:52:40
【问题描述】:

我写了一个自定义的HtmlHelper,如下:

public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null)
{
    if (routeValues == null && htmlAttributes != null)
        return htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId), actionName, controllerName, htmlAttributes);

    return htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId),
        actionName, controllerName,
        routeValues,
        htmlAttributes);
}

如果routeValueshtmlAttributes 都为空也没关系。
但如果htmlAttributes 有值且routeValues 为空,则将a 标签呈现如下:

<a comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" count="1" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" href="/Home/Login?Length=7">Exit</a>

这有什么问题?

【问题讨论】:

  • 我刚刚测试过,没有变化。
  • 和往常一样,我猜,错误重载stackoverflow.com/questions/4357856/…
  • 唯一接受string作为前3个参数的ActionLink重载还需要第4个参数作为路由值,第5个参数作为html属性,这意味着如果你的if块评估为真,您将 htmlAttributes 作为路由值。只需删除这个助手 - 它完全没有内置的 ActionLink() 助手没有做的事情。

标签: c# model-view-controller html-helper actionlink


【解决方案1】:

试试这个:

public static MvcHtmlString MdActionLink(this HtmlHelper htmlHelper, string resourceId, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null)
{
    if (routeValues == null)
         routeValues = new RouteValueDictionary();

     if (htmlAttributes == null)
          htmlAttributes = new Dictionary<string, object>();

     htmlHelper.ActionLink(ResourcesHelper.GetMessageFromResource(resourceId),
        actionName, controllerName,
        routeValues,
        htmlAttributes);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    相关资源
    最近更新 更多