MVC多级目录菜单 ----- 简单模拟
Model ---- cs
{ public class Class1 { public int ID{get;set;} public int parentID { get; set; } public int childID { get; set; } public string title { get; set; } }
MVC View ---- cshtml:
@using WebApplication1.Models; @{ ViewBag.Title = "About"; } <h2>@ViewBag.Title.</h2> <h3>@ViewBag.Message</h3> <p>Use this area to provide additional information.</p> @{ var totalList = new List<Class1>();//全部目录列表 totalList.Add(new Class1{ ID = 1,parentID = 0, title = "title_1"}); totalList.Add(new Class1{ ID = 5,parentID = 0, title = "title_5"}); totalList.Add(new Class1{ ID = 12,parentID = 0, title = "title_12"}); totalList.Add(new Class1{ ID = 2,parentID = 1, title = "title_2"}); totalList.Add(new Class1{ ID = 3,parentID = 1, title = "title_3"}); totalList.Add(new Class1{ ID = 4,parentID = 1, title = "title_4"}); totalList.Add(new Class1{ ID = 6,parentID = 5, title = "title_6"}); totalList.Add(new Class1{ ID = 7,parentID = 5, title = "title_7"}); totalList.Add(new Class1{ ID = 8,parentID = 5, title = "title_8"}); totalList.Add(new Class1{ ID = 9,parentID = 2, title = "title_9"}); totalList.Add(new Class1{ ID = 10,parentID = 2, title = "title_10"}); totalList.Add(new Class1{ ID = 11,parentID = 2, title = "title_11"}); totalList.Add(new Class1{ ID = 13,parentID = 12, title = "title_13"}); totalList.Add(new Class1{ ID = 14,parentID = 12, title = "title_14"}); totalList.Add(new Class1{ ID = 15,parentID = 12, title = "title_15"}); totalList.Add(new Class1{ ID = 16,parentID = 4, title = "title_16"}); totalList.Add(new Class1{ ID = 17,parentID = 4, title = "title_17"}); totalList.Add(new Class1{ ID = 18,parentID = 4, title = "title_18"}); foreach (var item in totalList){ <ul> @if (item.parentID == 0) {//如果为一级目录 <li>@item.title</li> if (totalList.Find(x => x.parentID == item.ID ) != null) { @Show(item); } } </ul> } } @helper Show(Class1 item){ var totalList = new List<Class1>();//全部目录列表 totalList.Add(new Class1 { ID = 1, parentID = 0, title = "title_1" }); totalList.Add(new Class1 { ID = 5, parentID = 0, title = "title_5" }); totalList.Add(new Class1 { ID = 12, parentID = 0, title = "title_12" }); totalList.Add(new Class1 { ID = 2, parentID = 1, title = "title_2" }); totalList.Add(new Class1 { ID = 3, parentID = 1, title = "title_3" }); totalList.Add(new Class1 { ID = 4, parentID = 1, title = "title_4" }); totalList.Add(new Class1 { ID = 6, parentID = 5, title = "title_6" }); totalList.Add(new Class1 { ID = 7, parentID = 5, title = "title_7" }); totalList.Add(new Class1 { ID = 8, parentID = 5, title = "title_8" }); totalList.Add(new Class1 { ID = 9, parentID = 2, title = "title_9" }); totalList.Add(new Class1 { ID = 10, parentID = 2, title = "title_10" }); totalList.Add(new Class1 { ID = 11, parentID = 2, title = "title_11" }); totalList.Add(new Class1 { ID = 13, parentID = 12, title = "title_13" }); totalList.Add(new Class1 { ID = 14, parentID = 12, title = "title_14" }); totalList.Add(new Class1 { ID = 15, parentID = 12, title = "title_15" }); totalList.Add(new Class1 { ID = 16, parentID = 4, title = "title_16" }); totalList.Add(new Class1 { ID = 17, parentID = 4, title = "title_17" }); totalList.Add(new Class1 { ID = 18, parentID = 4, title = "title_18" }); <ul> @for (int i = 0; i < totalList.Count; i++){ if (item.ID == totalList[i].parentID){ <li>@totalList[i].title</li> if (totalList.Find(x => x.parentID == totalList[i].ID) != null){ @Show(totalList[i]); } } } </ul> }
时间: 2024-11-15 13:06:34