Hi Brenden, I've finally got it working thanks to your and Peter's help. This -> .Children.WithAllData when looping through the nodes was key.
In my Controller:
public ActionResult GetMyAAAMenu()
{
var menuTreeNode = MenuProvider.GetMenuTreeNodes().FirstOrDefault();
var model = new MenuViewModel();
if (menuTreeNode.NodeHasChildren)
{
foreach (TreeNode childNode in menuTreeNode.Children.WithAllData)
{
if (childNode.ClassName == "AAACustom.Menu")
{
if (model.SubMenus == null)
model.SubMenus = new List<MenuViewModel>();
model.SubMenus.Add(GetMenuTree(childNode));
}
else
{
var menuItem = new Models.MenuItemViewModel
{
MenuItemText = childNode.GetValue<string>("MenuItemText", ""),
MenuItemRelativeUrl = childNode.GetValue<string>("MenuItemRelativeUrl", "").ToString(),
MenuItemIconUrl = childNode.GetValue<string>("MenuItemIconUrl", "").ToString(),
ExternalUrl = childNode.GetValue<string>("ExternalUrl", "").ToString()
};
if (model.MenuItems == null)
model.MenuItems = new List<Models.MenuItemViewModel>();
model.MenuItems.Add(menuItem);
}
}
}
return PartialView("_MyAAAMenu", model);
}
protected MenuViewModel GetMenuTree(TreeNode menuNode)
{
var menuViewModel = new MenuViewModel();
menuViewModel.MenuName = menuNode.GetValue("MenuName", "").ToString();
if (menuNode.NodeHasChildren)
{
foreach (TreeNode childNode in menuNode.Children.WithAllData)
{
if (childNode.ClassName == "AAACustom.Menu")
{
menuViewModel.SubMenus.Add(GetMenuTree(childNode));
}
else // ClassName = AAACustom.MenuItem
{
var menuItem = new AAA.Models.MenuItemViewModel
{
MenuItemText = childNode.GetValue("MenuItemText", "").ToString(),
MenuItemRelativeUrl = childNode.GetValue("MenuItemRelativeUrl", "").ToString(),
MenuItemIconUrl = childNode.GetValue("MenuItemIconUrl", "").ToString(),
ExternalUrl = childNode.GetValue("ExternalUrl", "").ToString()
};
if (menuViewModel.MenuItems == null)
menuViewModel.MenuItems = new List<Models.MenuItemViewModel>();
menuViewModel.MenuItems.Add(menuItem);
}
}
}
return menuViewModel;
}
And in my provider:
public static MultiDocumentQuery GetMenuTreeNodes()
{
return DocumentHelper.GetDocuments()
.Type("AAACustom.Menu", q => q
.Columns("TopMenuItemsID", "MenuName"))
.Type("AAACustom.MenuItem", q => q
.Columns("MenuItemID", "MenuItemText", "MenuItemPage", "MenuItemIcon", "ExternalUrl"))
.OnSite(SiteContext.CurrentSiteName)
.Path("/My-AAA-menu")
.OrderBy("NodeLevel", "NodeOrder", "NodeName")
.Published();
}
I'm sure there are other ways of doing this but thanks for helping me get to this point.