目前,我正在使用以下代码中的一个MVC 3应用程序,其中,
取决于点击的特定菜单项,我需要更改“LI”样式(“menu-active”和“menu-inactive”)。
div中包含的每个菜单项都有不同的类(例如HOME菜单有“divClassHome”样式,其他分别有其他)。
这里有任何html帮助器,因为我有20多个菜单项,因此代码是每组的复制粘贴,请建议!
if (ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString() == "Home" && ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString() == "Index")
{
<li class="menu-active">
<div class="DivClassHome">
@Html.ActionLink("Home", "Index", "Home", null, new { @class = "XYZ" })
</div>
} 其他 { @ Html.ActionLink(“Home”,“Index”,“Home”,null,new {@class =“XYZ”}) }
答案 0 :(得分:0)
结帐TwitterBootstrapMVC。它负责使用扩展方法.SetLinksActiveByControllerAndAction()
选择活动菜单项:
using (var nav = Html.Bootstrap().Begin(new Nav().SetLinksActiveByControllerAndAction()))
{
@nav.ActionLink("Link 1", "action1")
@nav.ActionLink("Link 2", "action2")
@nav.Link("External Link", "#")
}