ASP.net MVC - Navigation and highlighting the “current” link

前端 未结 9 1624
清酒与你
清酒与你 2020-12-30 11:10

When you create a new MVC project it creates a Site.master with the following markup:

    <
9条回答
  •  遥遥无期
    2020-12-30 11:27

    I Used this approach with a htmlhelper for the problem:

    public static class HtmlHelpers
    {
        public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper,
                                                string linkText,
                                                string actionName,
                                                string controllerName
                                            )
        {
    
            string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
    
            if (actionName.Equals(currentAction, StringComparison.InvariantCultureIgnoreCase) && controllerName.Equals(currentController, StringComparison.InvariantCultureIgnoreCase))
            {
                return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = "active" });
            }
    
            return htmlHelper.ActionLink(linkText, actionName, controllerName);
    
        }
    }
    

    and for the view

    @Html.MenuLink"Linktext", "action", "controller")
    

提交回复
热议问题