如何在视觉上指示在ASP.NET MVC当前页面? [英] How to visually indicate current page in ASP.NET MVC?

查看:113
本文介绍了如何在视觉上指示在ASP.NET MVC当前页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为讨论的基础。创建一个标准的ASP.NET MVC Web项目。

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>

如何设置指示当前页面的可视化CSS样式。
例如,在关于页/控制器的时候,我基本上是想做到这一点:

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>

和,当然,当在主页上

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

(有一个CSS样式名目前在视觉显示菜单,这是当前页面。)

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

我可以打破从母版页菜单DIV到内容占位符,但是这将意味着我必须把菜单每一页上。

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?

推荐答案

最简单的方法是让从ViewContext的的RouteData电流控制器和行动。注意签字和使用@变化逃脱关键字。

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" : "" ) %>

请注意,您可以结合这样的一个扩展的HtmlHelper像@乔恩的,并使其更干净。

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

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

在哪里MenuActionLink是

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 );
     }
}

这篇关于如何在视觉上指示在ASP.NET MVC当前页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆