MVC2多级导航

时间:2010-10-30 14:18:03

标签: asp.net-mvc

我还没有找到答案的一个问题是这个问题。如何在多个级别上跟踪活动的Sitemap节点?

例如:

  • 节点1
  • 节点2“有效”
         
    • 子节点1
    •    
    • 子节点2“活跃”
  • node 3

如何跟踪正在激活的子节点以及它所属的父节点被标记为活动状态。

我知道有一些名为IsDescendant的东西,但由于空值,我总是在使用它时会遇到异常。

是否有一些关于在mvc.net(或asp.net)中进行更高级自定义导航的截屏/教程。大多数似乎都处理简单的单级导航。

如果可能,我更喜欢C#中的示例,谢谢。

1 个答案:

答案 0 :(得分:1)

好的,,,我遇到了同样的问题,我想出了一个解决方法,在这个问题中检查我的答案:

Hierarchical menu in view based on parent child class

我一直想提出一种更通用的优雅方式,因为我遇到了这个问题,但从那时起我一直很忙。我希望我能在接下来的几周内找到一些时间来提出一些东西,,,


注意:为了简化我在方法DisplayMenuConvertToItem特殊性中的上述问题的解决方案,我删除了应该检查{的部分{1}}和currentMenuID字段,并将currentMenuItemID css类添加到li元素(或您的current类)。

由于这就是你的问题所在,我已经提供了以下完整的方法。

active

我不认为这是解决问题的完美解决方案,我讨厌在C#代码中编写HTML,我会尽力为这个问题提出一个更通用(多层次)的清洁解决方案,当然在框架中使用public static string DisplayMenu(this HtmlHelper helper, NavigationModel navigationMenu) { string classOfCurrentMenu; String result = "<ul id='main-nav'>\n"; foreach(Menu menu in navigationMenu.Menus) { classOfCurrentMenu = ""; if (menu.ID == navigationMenu.currentMenuID) { classOfCurrentMenu = "current"; } result += "<li>\n"; result += string.Format("<a href='#' class='{0}'> {1} </a>\n", helper.AttributeEncode(classOfCurrentMenu),helper.AttributeEncode(menu.Name)); result += "<ul>\n"; foreach(MenuItem item in menu.MenuItems) { result += NavigationHelper.ConvertToItem(helper, item, navigationMenu.currentMenuID, navigationMenu.currentMenuItemID); } result += "</ul>\n"; result += "</li>\n"; } result += "</ul>\n"; return result; } private static string ConvertToItem(this HtmlHelper helper,MenuItem item, int currentMenuID, int currentMenuItemID) { if (item.Show) { string classOfCurrentItem = ""; if (item.ID == currentMenuItemID && item.MenuID == currentMenuID) classOfCurrentItem = "current"; return string.Format("<li><a href='{0}' class='{1}'>{2}</a></li>\n", helper.AttributeEncode(item.Link), helper.AttributeEncode(classOfCurrentItem), helper.AttributeEncode(item.Label)); } else { return ""; } } 类。

相关问题