MVC ActionLink呈现错误的HTML?

时间:2015-05-09 12:42:41

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

我写了一个自定义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>

它出了什么问题?

1 个答案:

答案 0 :(得分: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);
}