有一个子导航如下:
<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类,以便我可以将其显示为选定/活动状态?
答案 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>