首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ASP.NET MVC中直观地显示当前页面?

如何在ASP.NET MVC中直观地显示当前页面?
EN

Stack Overflow用户
提问于 2009-09-24 12:25:20
回答 5查看 7K关注 0票数 17

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

它将在母版页中包含两个菜单项:

代码语言:javascript
复制
<div id="menucontainer">
  <ul id="menu">
    <li>
      <%= Html.ActionLink("Home", "Index", "Home")%></li>
    <li>
      <%= Html.ActionLink("About", "About", "Home")%></li>
  </ul>
</div>

如何设置指示当前页的可视CSS样式。例如,当在About页面/控制器中时,我基本上想这样做:

代码语言:javascript
复制
<%= Html.ActionLink("About", "About", "Home", new {class="current"})%></li>

当然,在主页上:

代码语言:javascript
复制
<%= Html.ActionLink("Home", "Index", "Home", new {class="current"})%></li>

( CSS样式命名为current,该样式在菜单中直观地指示这是当前页面。)

我可以将母版页中的菜单div分解为内容占位符,但这意味着我必须将菜单放在每个页面上。

有什么好主意吗?有没有很好的解决方案?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-09-24 12:35:50

最简单的方法是从ViewContext的RouteData获取当前控制器和操作。注意签名的变化,并使用@对关键字进行转义。

代码语言:javascript
复制
<% 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" : "" ) %>

请注意,您可以将其与@Jon这样的HtmlHelper扩展结合起来,使其更简洁。

代码语言:javascript
复制
<%= Html.MenuLink( "About", "About", "Home", null, null, "current" ) %>

MenuActionLink在哪里

代码语言:javascript
复制
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 );
     }
}
票数 24
EN

Stack Overflow用户

发布于 2009-09-24 13:50:59

我最近为此创建了一个HTML Helper,如下所示:

代码语言:javascript
复制
public static string NavigationLink(this HtmlHelper helper, string path, string text)
{
    string cssClass = String.Empty;
    if (HttpContext.Current.Request.Path.IndexOf(path) != -1)
    {
        cssClass = "class = 'selected'";
    }

    return String.Format(@"<li><a href='{0}' {1}>{2}</a></li>", path, cssClass, text);
}

其实现如下所示:

代码语言:javascript
复制
  <ul id="Navigation">
  <%=Html.NavigationLink("/Path1", "Text1")%>
  <%=Html.NavigationLink("/Path2", "Text2")%>
  <%=Html.NavigationLink("/Path3", "Text3")%>
  <%=Html.NavigationLink("/Path4", "Text4")%>
  </ul>
票数 1
EN

Stack Overflow用户

发布于 2011-06-30 02:06:17

如果您使用的是T4MVC,则可以使用以下命令:

代码语言:javascript
复制
        public static HtmlString MenuLink(
        this HtmlHelper helper,
        string text,
        IT4MVCActionResult action,
        object htmlAttributes = null)
    {
        var currentController = helper.ViewContext.RouteData.Values["controller"] as string ?? "home";
        var currentAction = helper.ViewContext.RouteData.Values["action"] as string ?? "index";

        var attributes = new RouteValueDictionary(htmlAttributes);
        var cssClass = (attributes.ContainsKey("class"))
                           ? attributes["class"] + " "
                           : string.Empty;

        string selectedClass;
        if(action.Controller.Equals(currentController, StringComparison.InvariantCultureIgnoreCase)
        {
            selectedClass = "selected-parent";
            if(action.Action.Equals(currentAction, StringComparison.InvariantCultureIgnoreCase))
                selectedClass = "selected";
        }
        cssClass += selectedClass;

        attributes["class"] = cssClass;

        return helper.ActionLink(text, (ActionResult)action, attributes);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1471362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档