我在_Layout.cshtml中定义了以下菜单,该菜单在我网站的所有页面中共享
<ul id="topnav" class="sf-menu">
<li>@Html.ActionLink("Home", "Index", "Home", null, new { @class = Html.ActivePage("Home", "Index") })</li>
<li>@Html.ActionLink("Services", "Services", "Home", null, new { @class = Html.ActivePage("Home", "Services") })
<ul>
<li>@Html.ActionLink("1", "1", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
<li>@Html.ActionLink("2", "2", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
<li>@Html.ActionLink("3", "3", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
<li>@Html.ActionLink("3", "4", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
</ul>
</li>
<li>@Html.ActionLink("FAQs", "FAQs", "Home", null, new { @class = Html.ActivePage("Home", "FAQs") })</li>
<li>@Html.ActionLink("About", "About", "Home", null, new { @class = Html.ActivePage("Home", "About") })</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", null, new { @class = Html.ActivePage("Home", "Contact") })</li>
</ul>
我的RouteConfig如下
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Services",
url: "{controller}/{action}",
defaults: new { controller = "Services", action = "Index" }
);
我的HTMLHelper定义如下
public static string ActivePage(this HtmlHelper helper, string controller, string action)
{
string classvalue = "";
string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
string currentAction = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();
if (currentController == controller && currentAction == action)
classvalue = "active";
return classvalue;
}
我希望能够做的是当用户选择“服务”菜单的任何子项时,“服务”菜单项被标记为“活动”。当我选择顶级菜单项但不选择子项时,代码将按原样运行。
任何人都可以对此提出任何建议。
TIA 特雷弗路易斯。