我正在尝试使用 DynamicNodeProvider 在 MVC3 中创建多级站点地图。但只显示根节点。请指导我代码中的问题所在。我已经定义了一个类来覆盖 GetDynamicNodeCollection,如下所示:
public class UserJourneyNavigationProvider : DynamicNodeProviderBase
{
private List<NavigationGroups> NavigationGroups { get; set; }
public UserJourneyNavigationProvider()
{
}
public UserJourneyNavigationProvider(List<NavigationGroups> NavigationGroups)
{
this.NavigationGroups = NavigationGroups;
}
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
// Build value
var returnValue = new List<DynamicNode>();
NavigationGroups ng1 = new NavigationGroups();
ng1.CategoryName = "Menu1";
NavigationGroups ng2 = new NavigationGroups();
ng2.CategoryName = "Menu2";
NavigationGroups ng3 = new NavigationGroups();
ng3.CategoryName = "Menu3";
NavigationGroups ng4 = new NavigationGroups();
ng4.CategoryName = "Menu4";
NavigationGroups ng5 = new NavigationGroups();
ng5.CategoryName = "Menu5";
NavigationGroups ng6 = new NavigationGroups();
ng6.CategoryName = "Menu6";
NavigationGroups ng7 = new NavigationGroups();
ng7.CategoryName = "Menu7";
NavigationGroups ng8 = new NavigationGroups();
ng8.CategoryName = "Menu8";
NavigationGroups ng9 = new NavigationGroups();
ng9.CategoryName = "Menu9";
NavigationGroups ng10 = new NavigationGroups();
ng10.CategoryName = "Menu10";
ng1.ChildCategory = new List<NavigationGroups>();
ng1.ChildCategory.Add(ng2);
ng1.ChildCategory.Add(ng3);
ng1.ChildCategory.Add(ng4);
ng5.ChildCategory = new List<NavigationGroups>();
ng5.ChildCategory.Add(ng6);
ng5.ChildCategory.Add(ng7);
ng5.ChildCategory.Add(ng8);
ng7.ChildCategory = new List<NavigationGroups>();
ng7.ChildCategory.Add(ng9);
ng7.ChildCategory.Add(ng10);
int i = 1;
ng1.CategoryId = (i++).ToString();
ng2.CategoryId = (i++).ToString();
ng3.CategoryId = (i++).ToString();
ng4.CategoryId = (i++).ToString();
ng5.CategoryId = (i++).ToString();
ng6.CategoryId = (i++).ToString();
ng7.CategoryId = (i++).ToString();
ng8.CategoryId = (i++).ToString();
ng9.CategoryId = (i++).ToString();
ng10.CategoryId = (i++).ToString();
NavigationGroups = new List<NavigationGroups>();
NavigationGroups.Add(ng1);
NavigationGroups.Add(ng5);
NavigationLinkGenerator(NavigationGroups, returnValue, "Home");
return returnValue;
}
private void NavigationLinkGenerator(List<NavigationGroups> navGroups, List<DynamicNode> returnValue, string parentKey)
{
foreach (NavigationGroups navGroup in navGroups)
{
DynamicNode node = new DynamicNode();
node.Controller = "Home";
node.Action = "ProcessActionLink";
node.Title = navGroup.CategoryName;
node.Description = navGroup.CategoryName;
node.ChangeFrequency = MvcSiteMapProvider.ChangeFrequency.Always;
node.Key = navGroup.CategoryId;
if (!string.IsNullOrWhiteSpace(parentKey))
node.ParentKey = parentKey;
if (navGroup.ChildCategory != null && navGroup.ChildCategory.Count > 0)
{
NavigationLinkGenerator(navGroup.ChildCategory, returnValue, navGroup.CategoryId);
}
returnValue.Add(node);
}
}
}
在 Mvc.sitemap 我这样定义节点:
<mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
<mvcSiteMapNode title="" controller="Home" action="ProcessActionLink" dynamicNodeProvider="Business.Layer.UserJourneyNavigationProvider, Business.Layer" />
</mvcSiteMapNode>
在 _Layout.cshtml 我有以下代码:
@Html.MvcSiteMap().Menu(false, true, true)