Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visually indicate current page in ASP.NET MVC?

As a base for discussion. Create a standard ASP.NET MVC Web project.

It will contain two menu items in the master page:

<div id="menucontainer">
  <ul id="menu">
    <li>
      <%= Html.ActionLink("Home", "Index", "Home")%></li>
    <li>
      <%= Html.ActionLink("About", "About", "Home")%></li>
  </ul>
</div>

How can I set the visual CSS style indicating the current page. For example, when in the About page/controller, I essentially would like to do this:

<%= Html.ActionLink("About", "About", "Home", new {class="current"})%></li>

And, of course, when on the home page:

<%= Html.ActionLink("Home", "Index", "Home", new {class="current"})%></li>

(Having a CSS style names current that visually indicates in the menu that this is the current page.)

I could break out the menu div from the master page into a content place holder, but that would mean that I must put the menu on every page.

Any ideas, is there a nice solution to this?

like image 490
Magnus Johansson Avatar asked Sep 24 '09 12:09

Magnus Johansson


1 Answers

The easiest way is to get the current controller and action from the ViewContext's RouteData. Note the change in signature and use of @ to escape the keyword.

<% var controller = ViewContext.RouteData.Values["controller"] as string ?? "Home";
   var action = ViewContext.RouteData.Values["action"] as string ?? "Index";
   var page = (controller + ":" + action).ToLower();
 %>

<%= Html.ActionLink( "About", "About", "Home", null,
                     new { @class = page == "home:about" ? "current" : "" ) %>
<%= Html.ActionLink( "Home", "Index", "Home", null,
                     new { @class = page == "home:index" ? "current" : "" ) %>

Note that you could combine this an HtmlHelper extension like @Jon's and make it cleaner.

<%= Html.MenuLink( "About", "About", "Home", null, null, "current" ) %>

Where MenuActionLink is

public static class MenuHelperExtensions
{
     public static string MenuLink( this HtmlHelper helper,
                                    string text,
                                    string action,
                                    string controller,
                                    object routeValues,
                                    object htmlAttributes,
                                    string currentClass )
     {
         RouteValueDictionary attributes = new RouteValueDictionary( htmlAttributes );
         string currentController = helper.ViewContext.RouteData.Values["controller"] as string ?? "home";
         string currentAction = helper.ViewContext.RouteData.Values["action"] as string ?? "index";
         string page = string.Format( "{0}:{1}", currentController, currentAction ).ToLower();
         string thisPage = string.Format( "{0}:{1}", controller, action ).ToLower();
         attributes["class"] = (page == thisPage) ? currentClass : "";
        return helper.ActionLink( text, action, controller, new RouteValueDictionary( routeValues ), attributes );
     }
}
like image 67
tvanfosson Avatar answered Sep 19 '22 14:09

tvanfosson