如何使用ViewContext?
How to use ViewContext?
我在 asp.net MVC 应用程序中有布局页面,此页面包含一个导航栏
<ul class="sidenavbar">
<li><a style="font-size:large">ASP.NET Tutorial</a></li>
<li>@Html.ActionLink("Home","Home")</li>
<li>@Html.ActionLink("Introduction", "Introduction")</li>
<li>@Html.ActionLink("Getting Started", "GettingStarted")</li>
</ul>
看到我的 navbar 中的每个项目都有 actionmethod 现在我想应用 css class active to currently selected list item 怎么实现这使用 ViewContext 或者是否有更好的方法?
这可能有效
<ul class="nav navbar-nav">
<li class="@(ViewContext.RouteData.Values["controller"].ToString() == "Home" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@(ViewContext.RouteData.Values["controller"].ToString() == "About" ? "active" : "")">@Html.ActionLink("About", "About", "Home")</li>
<li class="@(ViewContext.RouteData.Values["controller"].ToString() == "Contact" ? "active" : "")">@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
我在 asp.net MVC 应用程序中有布局页面,此页面包含一个导航栏
<ul class="sidenavbar">
<li><a style="font-size:large">ASP.NET Tutorial</a></li>
<li>@Html.ActionLink("Home","Home")</li>
<li>@Html.ActionLink("Introduction", "Introduction")</li>
<li>@Html.ActionLink("Getting Started", "GettingStarted")</li>
</ul>
看到我的 navbar 中的每个项目都有 actionmethod 现在我想应用 css class active to currently selected list item 怎么实现这使用 ViewContext 或者是否有更好的方法?
这可能有效
<ul class="nav navbar-nav">
<li class="@(ViewContext.RouteData.Values["controller"].ToString() == "Home" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@(ViewContext.RouteData.Values["controller"].ToString() == "About" ? "active" : "")">@Html.ActionLink("About", "About", "Home")</li>
<li class="@(ViewContext.RouteData.Values["controller"].ToString() == "Contact" ? "active" : "")">@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>