我是初学程序员。我试图显示层次结构,但没有显示一个嵌套的子菜单。我想我的逻辑中有一个错误,但无法找到。任何人都想指出在UI中没有显示一个名为 child 4 的子菜单的区域。
以下是代码
public ActionResult Index()
{
List<MenuItem> mi = 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}
};
ViewBag.menusList = mi;
return View();
}
public class MenuItem
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
}
剃刀代码
@{ var menusList = ViewBag.menusList as List<Scaffolding.Controllers.MenuItem>; }
@if (menusList != null)
{
<ul id="menu">
@foreach(var parentMenu in menusList.Where(p => p.ParentId == 0))
{
<li>
<span>@parentMenu.Name</span>
@if (menusList.Count(p => p.ParentId == parentMenu.Id) > 0)
{
<ul id="menu">
@foreach(var childMenu in menusList.Where(p => p.ParentId == parentMenu.Id))
{
<li>
<span>@childMenu.Name</span>
</li>
}
</ul>
}
</li>
}
</ul>
}
提前感谢。
我已经更改了代码,现在收到了名为
的错误CS1502:最佳重载方法匹配 'ASP._Page_Views_Menu_Index_cshtml.ShowTree(System.Collections.Generic.List)' 有一些无效的论点
我的代码仍无效。这是更新的代码
@{
var menuList = ViewBag.menusList as List<Scaffolding.Controllers.MenuDTO>;
ShowTree(menuList);
}
@helper ShowTree(List<Scaffolding.Controllers.MenuDTO> menusList)
{
if (menusList != null)
{
foreach (var item in menusList)
{
<li>
<span>@item.Name</span>
@if (item.Children.Any())
{
<ul>
@ShowTree(item.Children)
</ul>
}
</li>
}
}
}
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<MenuDTO> mi = allMenu
.Select(e => new
{
Id = e.Id,
Name = e.Name,
ParentId = e.ParentId,
Children = allMenu.Where(x => x.ParentId == e.Id).ToList()
}).ToList()
.Select(p => new MenuDTO
{
Id = p.Id,
Name = p.Name,
ParentId = p.ParentId,
Children = allMenu.Where(x => x.ParentId == p.Id).ToList()
}).ToList();
ViewBag.menusList = mi;
return View();
}
public class MenuItem
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public virtual ICollection<MenuItem> Children { get; set; }
}
public class MenuDTO
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public virtual ICollection<MenuItem> Children { get; set; }
}
答案 0 :(得分:0)
我想发布完整的工作代码。
@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);
}
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();
}
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; }
}