I am trying to show nested data in ul/li, but nested children are not showing. See my code and please tell me what is wrong there.
Controller:
public ActionResult Index()
{
List<MenuItem> allMenu = new List<MenuItem>
{
new MenuItem {Id=1,Name="Parent 1", ParentId=0},
new MenuItem {Id=2,Name="child 1", ParentId=1},
new MenuItem {Id=3,Name="child 2", ParentId=1},
new MenuItem {Id=4,Name="child 3", ParentId=1},
new MenuItem {Id=5,Name="Parent 2", ParentId=0},
new MenuItem {Id=6,Name="child 4", ParentId=4}
};
List<MenuItem> mi = allMenu
.Where(e => e.ParentId == 0) /* grab only the root parent nodes */
.Select(e => new MenuItem
{
Id = e.Id,
Name = e.Name,
ParentId = e.ParentId,
Children = allMenu.Where(x => x.ParentId == e.Id).ToList()
}).ToList();
ViewBag.menusList = mi;
return View();
}
POCO class:
public class MenuItem
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public virtual List<MenuItem> Children { get; set; }
}
View:
@helper ShowTree(List<Scaffolding.Controllers.MenuItem> menusList)
{
<ul>
@foreach (var item in menusList)
{
<li>
<span>@item.Name</span>
@if (item.Children != null && item.Children.Any())
{
@ShowTree(item.Children)
}
</li>
}
</ul>
}
@{
var menuList = ViewBag.menusList as List<Scaffolding.Controllers.MenuItem>;
@ShowTree(menuList);
}
If you run the code then you will see child 4 is not showing which is a child of child 3. Please advise what I need to change in my code. Thanks