有很多方法可以显示动态生成的菜单,我将发布我的方法,我有两个类 Menu 和 MenuItem (菜单只是 MenuItems 列表的包装,MenuItems 是实际的>“真实”链接),然后我有一个 NavigationViewModel 来包装所有内容。
public class MenuItem
{
public int MenuID { get; set; }
public int ID { get; set; }
public String Label { get; set; }
public String Link { get; set; }
public Boolean Show { get; set; }
public MenuItem(int menuId, int id, string label, string link, Boolean show)
{
this.MenuID = menuId;
this.ID = id;
this.Label = label;
this.Link = link;
this.Show = show;
}
}
//
public class Menu
{
public int ID { get; set; }
public string Name { get; set; }
public List<MenuItem> MenuItems { get; set; }
public Menu(int id, string name)
{
this.ID = id;
this.Name = name;
this.MenuItems = new List<MenuItem>();
}
}
// 这是包装所有内容的 NavigationModel。
public class NavigationModel
{
public int currentMenuID { get; set; }
// used to determine the current displayed Menu to add
// the "current" class to it. (to be set in the controller)
public int currentMenuItemID { get; set; }
// used to determine the current displayed MenuItem to add
// the "current" class to it. (to be set in the controller)
public List<Menu> Menus { get; set; }
public NavigationModel()
{
this.Menus = new List<Menu>();
// Set Default Menu ( Menu 1 )
this.currentMenuID = 1;
// Set Default Menau Item ( None )
this.currentMenuItemID = 0;
}
}
// 这是构建 NavigationModel 的方法示例。
private static NavigationModel BuildNavigationMenu(User currentUser, string rootURL)
{
string loginURL = rootURL + "Account/LogOn";
// Main Menu
Menu MainMenu = new Menu(1, "Home");
MainMenu.MenuItems.Add(new MenuItem(1, 1, "Welcome", rootURL, true));
MainMenu.MenuItems.Add(new MenuItem(1, 2, "How It Works", rootURL + "Home/HowDoesItWork", true));
// Work Menu
Menu WorkMenu = new Menu(2, "Work");
WorkMenu.MenuItems.Add(new MenuItem(2, 1, "Profile", rootURL + "User/Profile/" + currentUser.ID , true));
WorkMenu.MenuItems.Add(new MenuItem(2, 2, "Customers", "#", true));
// Add Menus To Navigation Model
NavigationModel navigationMenu = new NavigationModel();
navigationMenu.Menus.Add(MainMenu);
navigationMenu.Menus.Add(HireMenu);
return navigationMenu;
}
// 我有一个 NavigationHelper,它负责使用以下两个方法输出 HTML。
注意:(这是一个简化版本,在我的实现中,我使用 Menu 和 MenuItem 的 id 来定位正在显示的当前菜单并向其添加“当前”CSS 类”
public static string DisplayMenu(this HtmlHelper helper, NavigationModel navigationMenu)
{
public static string DisplayMenu(this HtmlHelper helper, NavigationModel navigationMenu)
{
String result = "<ul id='main-nav'>\n";
foreach(Menu menu in navigationMenu.Menus)
{
result += "<li>\n";
result += string.Format("<a href='#'> {0} </a>\n",helper.AttributeEncode(menu.Name));
result += "<ul>\n";
foreach(MenuItem item in menu.MenuItems)
{
result += NavigationHelper.ConvertToItem(helper, item);
}
result += "</ul>\n";
result += "</li>\n";
}
result += "</ul>\n";
return result;
}
private static string ConvertToItem(this HtmlHelper helper,MenuItem item)
{
if (item.Show)
{
return string.Format("<li><a href='{0}'>{1}</a></li>\n", helper.AttributeEncode(item.Link), helper.AttributeEncode(item.Label));
}
else { return ""; }
}
}
最后,我在 Master 中进行了以下调用来显示菜单。
<!-- Navigation -->
<%= Html.DisplayMenu(Model.NavigationMenu) %>
注意:我有一个强类型的 MasterPage,其类型 (BaseViewModel) 包含 NavigationModel 类型的属性 NavigationMenu
将一切连接在一起。
在我的示例中,我必须在每个操作方法中为我的视图提供 ViewModel (继承我的 BaseViewModel ),然后构建菜单(使用完成大部分工作的生成器方法,这样我就不必重新输入它每个动作方法)。如果您不使用 BaseViewModel,那么您将需要找到另一种方法来构建导航模型,然后将导航模型传递到您的母版页。
对我来说,我发现有一个强类型的 MasterPage 可以让事情变得更简单、更干净。怀尔德王有一个很好的方法来在他的高尔夫追踪器系列 http://www.mvccentral.net/Article/Details/3/Golf_Tracker_-_Pt_1_-_Overview.