Here is basically how I did it, just cache the call to get this data. Might be an easier way to do this but I had some other needs to do it this way.
public static List<TreeNode> GetPages()
{
var selparams = new NodeSelectionParameters
{
//Page Types to show in Nav Menu
ClassNames = "Custom.Page;Custom.OtherPageType",
Where = "NodeLevel = 1",
SelectOnlyPublished = true
};
return DocumentHelper.GetDocuments(selparams, new TreeProvider()).Items.ToList();
}
private List<NavLinks> GetLinks()
{
var pages = PageHelper.GetPages();
var result = new List<NavLinks>();
foreach (var page in pages)
{
if (page.GetValue("ShowInMainNav", false))
{
var children = new List<NavLinks>();
if (page.Children.Count > 0)
{
children = GetChildren(page);
}
var url = page.NodeAliasPath;
result.Add(new NavLinks
{
Children = children,
DisplayName = page.GetValue("DisplayName",""),
Url = url
});
}
}
return result;
}
private List<NavLinks> GetChildren(TreeNode page)
{
var result = new List<NavLinks>();
var children = page.Children.WithAllData;
foreach (var child in children)
{
if (child.GetValue("ShowInMainNav", false))
{
var subchildren = new List<NavLinks>();
if (child.Children.Count > 0)
{
subchildren = GetChildren(child);
}
var url = child.NodeAliasPath;
result.Add(new NavLinks
{
Children = subchildren,
DisplayName = child.GetValue("DisplayName", ""),
Url = url
});
}
}
return result;
}
And in the view:
@helper Menu(NavLinks link)
{
var links = link.Children;
<ul class="submenu">
<li><a class="" href="@link.Url">Overview</a></li>
@foreach (var child in links)
{
<li>
<a class="" href="@child.Url">@child.DisplayName</a>
@if (child.Children.Count > 0)
{
@Menu(child)
}
</li>
}
</ul>
}
@foreach (var link in Model.Links)
{
<li class="">
<a class="" href="@link.Url">
@link.DisplayName
</a>
@if (link.Children.Count > 0)
{
@Menu(link)
}
</li>
}