ASP.NET MVC - Current Page highlighting in navigation

前端 未结 5 484
星月不相逢
星月不相逢 2020-12-08 11:46

I\'m wondering how is it possible to add a CSS Class to the current page in your navigation when using ASP.NET MVC 3? Here is my navigation in my _Layout.cshtml file:

<
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 12:22

    You can do this

    @{ 
       var currentController = ViewContext.RouteData.Values["controller"] as string ?? "Home";
       var currentAction = ViewContext.RouteData.Values["action"] as string ?? "Index";
       var currentPage = (currentController + "-" + currentAction ).ToLower();
    }
    
    @Html.ActionLink("Product Search", "Index", "Home", null,
                     new { @class = currentPage == "home-index" ? "current" : "" })
    @Html.ActionLink("MyAccount", "MyAccount", "Account", null,
                      new { @class = currentPage == "account-myaccount" ? "current" : "" })
    

提交回复
热议问题