ActionLink扩展方法MVC5中的htmlAttributes

时间:2016-07-08 11:34:18

标签: asp.net-mvc razor html-helper

我使用扩展方法在菜单上的活动链接上维护css类。

但是我遇到了htmlAttributes和对象值导致错误的问题。

我在Razor页面中有以下内容,但我不明白我是如何解析htmlAttributes的。

@Html.MenuLink("Summary", "Summary", "Graphs", null, new { @class = "dropdown-toggle caret", data_target = "#", data_toggle = "dropdown" })

通过查看HtmlHelper,该方法应该具有IDictionary<object, string>作为htmlAttributes的类型。新的{ @class = "dropdown-toggle caret", data_target = "#", data_toggle = "dropdown" }语法对于字典来说不是典型的,所以这是正确的吗?

显然,我做错了,因为它返回了以下错误:

Argument 6: cannot convert from '<anonymous type: string class, string data_target, string data_toggle>' to 'System.Collections.Generic.IDictionary<object, string>'

扩展方法I我试图在下面工作:

public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string text, string action, string controller, RouteValueDictionary routeValues, IDictionary<object, string> htmlAttributes)
        {
            var routeData = htmlHelper.ViewContext.RouteData.Values;

            var currentController = routeData["controller"];
            var currentAction = routeData["action"];

            if (string.Equals(action, currentAction as string, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
            {
                return htmlHelper.ActionLink(text, action, controller, null, new { @class = "currentMenu" });
            }

            return htmlHelper.ActionLink(text, action, controller);
        }

1 个答案:

答案 0 :(得分:6)

将参数从IDictionary<object, string> htmlAttributes更改为object htmlAttributes,因为您将属性作为对象传递。

然后,您可以使用

转换对象
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

但是,在扩展方法中没有使用属性的地方。您的所有生成都是class = "currentMenu",具体取决于当前的控制器和操作名称。如果您打算添加属性加上类名(取决于条件),则可以使用

attributes.Add("class", "currentMenu");

允许定义路由值和html属性以及有条件地包含"currentMenu"类名称的完整方法应该是

public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string text, string action, string controller, object routeValues, object htmlAttributes)
{
    var routeData = htmlHelper.ViewContext.RouteData.Values;
    string currentController = (string)routeData["controller"];
    string currentAction = (string)routeData["action"];
    if (string.Equals(action, currentAction, StringComparison.OrdinalIgnoreCase) && string.Equals(controller, currentController, StringComparison.OrdinalIgnoreCase))
    {
        if (htmlAttributes == null)
        {
            return htmlHelper.ActionLink(text, action, controller, routeValues, new { @class = "currentMenu" });
        }
        else
        {
            // convert object to RouteValueDictionary
            var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            if (attributes.ContainsKey("class"))
            {
                // append the class name
                attributes["class"] = string.Format("{0} currentMenu", attributes["class"]);
            }
            else
            {
                // add the class name
                attributes.Add("class", "currentMenu");
            }
            return htmlHelper.ActionLink(text, action, controller, new RouteValueDictionary(routeValues), attributes);
        }
    }
    return htmlHelper.ActionLink(text, action, controller, routeValues, htmlAttributes);
}

附注:您还应考虑包含其他重载,以便根据内置RouteValueDictionary routeValues方法接受IDictionary<String, Object>) htmlAttributesActionLink(),您可以检查source code以查看各种重载如何落到其他重载上。