【问题标题】:htmlAttributes in ActionLink Extension method MVC5ActionLink 扩展方法 MVC5 中的 htmlAttributes
【发布时间】:2016-07-08 11:34:18
【问题描述】:

我正在使用扩展方法在菜单上的活动链接上维护一个 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>'

我正在尝试的扩展方法如下:

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

【问题讨论】:

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


    【解决方案1】:

    将参数从IDictionary&lt;object, string&gt; 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);
    }
    

    旁注:您还应该考虑根据内置的ActionLink() 方法包括其他重载以接受RouteValueDictionary routeValuesIDictionary&lt;String, Object&gt;) htmlAttributes,并且您可以检查source code 以查看各种重载如何通过其他重载。

    【讨论】:

    • 太棒了 - 谢谢,是的,扩展方法不完整,我正在研究代码并试图弄清楚 HtmlHelper 是如何实现它的。
    • 如果您可能在对象参数中传递其他类名,那么您还需要检查if(attributes.Contains("class") 并将额外的“currentMenu”类添加到现有类名中。现在没时间了,但我会在早上添加完整的代码,包括指向源代码的链接,这将有助于解释如何创建各种重载。
    • 很高兴看到它来自哪里,非常感谢!
    • 这很好,感谢源代码,在 F12 之间闪烁,可能错过了加载,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多