MVC ActionLink呈现错误的HTML



我写了一个自定义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为null,则呈现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>

它怎么了?

尝试以下:

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);
}

最新更新