li 元素 class 与 ASP.NET MVC 的条件设置
Conditional setting of li element class with ASP.NET MVC
我在 /Shared/MainMenu.cshtml:
中有一个简单的菜单
<ul class="nav navbar-nav">
<li class="active">@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Features", "Index", "Features", "active")</li>
<li>@Html.ActionLink("Faq", "Index", "Faq")</li>
<li>@Html.ActionLink("Help", "Index", "Help")</li>
</ul>
我想指示当前页面,但在 Index.cshtml 中设置一个值,并将相关
元素的 class 设置为 "active",只是正如上面 "Home" 菜单项所具有的那样。
设置相关
的class而不设置其他的最简单的方法是什么?
也许吧?
@{
String controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
}
<ul class="nav navbar-nav">
<li class="@(controller == "Home" ? "active": "")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@(controller == "Features" ? "active": "")">@Html.ActionLink("Features", "Index", "Features", "active")</li>
<li class="@(controller == "Faq" ? "active": "")">@Html.ActionLink("Faq", "Index", "Faq")</li>
<li class="@(controller == "Help" ? "active": "")">@Html.ActionLink("Help", "Index", "Help")</li>
</ul>
我在 /Shared/MainMenu.cshtml:
中有一个简单的菜单 <ul class="nav navbar-nav">
<li class="active">@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Features", "Index", "Features", "active")</li>
<li>@Html.ActionLink("Faq", "Index", "Faq")</li>
<li>@Html.ActionLink("Help", "Index", "Help")</li>
</ul>
我想指示当前页面,但在 Index.cshtml 中设置一个值,并将相关
设置相关
也许吧?
@{
String controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
}
<ul class="nav navbar-nav">
<li class="@(controller == "Home" ? "active": "")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@(controller == "Features" ? "active": "")">@Html.ActionLink("Features", "Index", "Features", "active")</li>
<li class="@(controller == "Faq" ? "active": "")">@Html.ActionLink("Faq", "Index", "Faq")</li>
<li class="@(controller == "Help" ? "active": "")">@Html.ActionLink("Help", "Index", "Help")</li>
</ul>