解决方法
在MVC之前,我查看了文件路径,并找出哪个标签是currrent.现在这很简单,您可以根据当前控制器分配当前选项卡.
一探究竟 …
大多数工作发生在用户控件中.
public partial class AdminNavigation : ViewUserControl { /// <summary> /// This hold a collection of controllers and their respective "tabs." Each Tab should have at least one controller in the collection. /// </summary> private readonly IDictionary<Type,string> dict = new Dictionary<Type,string>(); public AdminNavigation() { dict.Add(typeof(BrandController),"catalog"); dict.Add(typeof(CatalogController),"catalog"); dict.Add(typeof(GroupController),"catalog"); dict.Add(typeof(ItemController),"catalog"); dict.Add(typeof(ConfigurationController),"configuration"); dict.Add(typeof(CustomerController),"customer"); dict.Add(typeof(DashboardController),"dashboard"); dict.Add(typeof(OrderController),"order"); dict.Add(typeof(WebsiteController),"website"); } protected string SetClass(string linktocheck) { Type controller = ViewContext.Controller.GetType(); // We need to determine if the linktocheck is equal to the current controller using dict as a Map string dictValue; dict.TryGetValue(controller,out dictValue); if (dictValue == linktocheck) { return "current"; } return ""; } }
然后在您的.ascx部分usercontol调用SetClass方法来检查与dict相关的链接.像这样:
<li class="<%= SetClass("customer") %>"><%= Html.ActionLink<CustomerController>(c=>c.Index(),"Customers",new{@class="nav_customers"}) %></li>
现在你需要的是CSS来突出显示你当前的标签.有一些不同的方法来做到这一点,但是你可以在这里开始一些想法:http://webdeveloper.econsultant.com/css-menus-navigation-tabs/
哦,别忘了把用户控件放在你的页面上(或MasterPage)…
<% Html.RenderPartial("AdminNavigation"); %>