根据页面激活导航项

时间:2013-04-13 07:37:55

标签: css asp.net-mvc asp.net-mvc-3 twitter-bootstrap

有一个子导航如下:

<div class="well sidebar-nav span3">
    <ul class="nav nav-list bs-docs-sidenav">
        <li><a href="/bio"><i class="icon-chevron-right"></i>Bio</a></li>
        <li><a href="/resume"><i class="icon-chevron-right"></i>Resume</a></li> 
    </ul>
</div>

正在加载:

@{Html.RenderAction("SubNav");}

调用非控制器操作:

    [ChildActionOnly]
    public ActionResult SubNav()
    {
        return PartialView();
    }

现在基于页面,如何将active添加到subnav中的li类,以便我可以将其显示为选定/活动状态?

1 个答案:

答案 0 :(得分:1)

您可以使用操作名称。在运行时查找操作名称,将其传递给模型或ViewBag查看。

 [ChildActionOnly]
    public ActionResult SubNav()
    {
            var rd = Request.RequestContext.RouteData;
            string currentAction = rd.GetRequiredString("action");
            string currentController = rd.GetRequiredString("controller");
            string currentArea = rd.Values["area"] as string;
            ViewBag.ActionName = currentAction;
            return PartialView();
    }

在视图中:

<div class="well sidebar-nav span3">
    <ul class="nav nav-list bs-docs-sidenav">
        <li><a href="/bio"><i class="icon-chevron-right  @(ViewBag.ActionName == "bio" ? "ActiveClass" : "")" ></i>Bio</a></li>
        <li><a href="/resume"><i class="icon-chevron-right  @(ViewBag.ActionName == "resume" ? "ActiveClass" : "")"></i>Resume</a></li> 
</ul>