Adding “active” tag to navigation list in an asp.net mvc master page

后端 未结 15 1919
渐次进展
渐次进展 2020-12-12 13:31

In the default asp.net mvc project, in the Site.Master file, there is a menu navigation list:

15条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 14:21

    You can also try to detect which is the current selected tab from its controller name and view name, then add the class attribute.

    public static string MenuActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName)
    {
        var htmlAttributes = new RouteValueDictionary();
    
        if (helper.ViewContext.Controller.GetType().Name.Equals(controllerName + "Controller", StringComparison.OrdinalIgnoreCase))
        {
            htmlAttributes.Add("class", "current");
        }
    
        return helper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(), htmlAttributes);
    }
    

提交回复
热议问题