解决方法
处理这个问题的最好方法是编写一个HTML帮助器。您可以使用RouteData.Values [“action”]获取当前执行的操作,并与菜单名称进行比较,如果它们匹配,则应用将突出显示的CSS类。
public static MvcHtmlString MenuItem( this HtmlHelper htmlHelper,string action,string text ) { var menu = new TagBuilder("div"); var currentAction = (string)htmlHelper.ViewContext.RouteData.Values["action"]; if (string.Equals( currentAction,action,StringComparison.CurrentCultureIgnoreCase) ) { menu.AddCssClass("highlight"); } menu.SetInnerText(text); return MvcHtmlString.Create(menu.ToString()); }
然后使用这个帮助器来渲染菜单项:
<%: Html.MenuItem("about","About us") %> <%: Html.MenuItem("contact","Contact us") %> ...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。