You can make a few changes:
-
Add attitional attibute into MenuItemViewModel to store Children:
// children
public IEnumerable<MenuItemViewModel> Children { get; set; }
-
Add a new method GetMenuChildren() to generate a tree of MenuItemViewModel items instead of list.
private IEnumerable<MenuItemViewModel> GetMenuChildren(IEnumerable<MenuItem> menuItems, int itemParentId, MultiDocumentQuery pages, int menuDepth = 0, int menuLevel = 0)
{
var items = menuItems
.Where(x => x.NodeParentID == itemParentId)
.Select(item => new MenuItemViewModel()
{
Children = GetMenuChildren(menuItems, item.NodeID, pages, menuDepth, menuLevel + 1),
MenuItemText = item.MenuItemText,
// Gets the URL for the page whose GUID matches the given menu item's selected page
MenuItemRelativeUrl = pages.FirstOrDefault(page => page.NodeGUID == item.MenuItemPage).RelativeURL
});
return items;
}
-
Call GetMenuChildren() method from GetMenu() method to get a menu model:
public virtual ActionResult GetMenu()
{
// Loads all menu items using the page type's generated provider
// Uses the menu item order from the content tree in the Kentico 'Pages' application
var menuItems = MenuItemProvider.GetMenuItems()
.Columns("NodeID", "NodeParentID", "MenuItemText", "MenuItemPage")
.OrderBy("NodeOrder")
.ToList();
// Loads the pages selected within the menu items
// The data only contains values of the NodeGUID identifier column
var pages = DocumentHelper.GetDocuments()
.WhereIn("NodeGUID", menuItems.Select(item => item.MenuItemPage).ToList())
.Columns("NodeGUID");
// Get Navigation Menu Root Item - should be the one
var menuItemsRoot = MenuItemsProvider.GetMenuItems().First();
// Creates a collection of view models based on the menu item and page data
var model = GetMenuChildren(menuItems, menuItemsRoot.NodeID, pages, 0);
return PartialView(MVCConsts.Menu.Views.ViewNames._SiteMenu, model);
}
Note: MenuItemsProvider (additional MenuItems Page Type added) is required to get the root item id.