ASP.NET MVC - Current Page highlighting in navigation

前端 未结 5 476
星月不相逢
星月不相逢 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:05
    @{
       var controller = ViewContext.RouteData.Values["controller"].ToString();
       var action = ViewContext.RouteData.Values["action"].ToString();
       var isActiveController = new Func<string, string, string, string, string>((ctrl, act, activeStyle, inactiveStyle) => controller == ctrl && action == act ? activeStyle : inactiveStyle);
     }
    

    Then in your class attribute in your HTML you can do:

    class="@isActiveController("controlername","action","activecssstyleclass","inactiveccsstyle")"
    

    Just an other way of @dknaack his answer.. bit more generic and less functionality to repeat in your code.

    0 讨论(0)
  • 2020-12-08 12:07

    I would recommend using an extension method for this. Something like:

    public static HtmlString NavigationLink(
        this HtmlHelper html,
        string linkText,
        string actionName,
        string controllerName)
    {
        string contextAction = (string)html.ViewContext.RouteData.Values["action"];
        string contextController = (string)html.ViewContext.RouteData.Values["controller"];
    
        bool isCurrent =
            string.Equals(contextAction, actionName, StringComparison.CurrentCultureIgnoreCase) &&
            string.Equals(contextController, controllerName, StringComparison.CurrentCultureIgnoreCase);
    
        return html.ActionLink(
            linkText,
            actionName,
            controllerName,
            routeValues: null,
            htmlAttributes: isCurrent ? new { @class = "current" } : null);
    }
    

    Then you can use it in your View by including the namespace of your extension and just calling your method:

    @using MyExtensionNamespace;
    
    ...
    
      @Html.NavigationLink("Product Search", "Index", "Home")
    | @Html.NavigationLink("Orders", "Index", "Orders") 
    | @Html.NavigationLink("My Account", "MyAccount", "Account")
    | @Html.NavigationLink("Logout", "LogOff", "Account")
    

    This has the benefit of keeping your razor a little cleaner and is easily reusable in other views.

    0 讨论(0)
  • 2020-12-08 12:07

    I used this tutorial to get this done, it's a lot simpler to understand and takes 2 minutes Hightlight Active menu item

    0 讨论(0)
  • 2020-12-08 12:11

    In my case,assume I have a Home page and a menu.

    Add a ViewBag.Active as a placeholder in Home page like this:

    @{
       ViewBag.Title = "Home";
       ViewBag.Active = "Home";
    }
    

    Then place it to your li class as a condition to active it or not:

     <li class="@(ViewBag.Active=="Home"? "active" : "")">
          <a href="@Url.Action("Index", "Home")"><span>@ViewBag.Title</span></a>
     </li>
    
    0 讨论(0)
  • 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" : "" })
    
    0 讨论(0)
提交回复
热议问题