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

前端 未结 9 1640
清酒与你
清酒与你 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:17

    First Make a Helper Class and HTML Helper method

     public static string IsActive(this HtmlHelper html,string control,string action)
        {
            var routeData = html.ViewContext.RouteData;
    
            var routeAction = (string)routeData.Values["action"];
            var routeControl = (string)routeData.Values["controller"];
    
            // both must match
            var returnActive = control == routeControl &&
                               action == routeAction;
    
            return returnActive ? "active" : "";
        }
    

    And in View or Layour Section Just Call the Helper Method with appropriate Conntroller and Action.

      @using YourNamespace.HtmlHelpermethodName
    
     
    

    this will add "active" string in class attribute and it will show like

     
    

提交回复
热议问题