问题描述
我有一个对象列表,我的.cshtml网页上的模式。
@model IEnumerable的< someObject>
每个对象都有名称,ID和PARENTID。
现在我显示的数据为简单的列表:
< UL>
@foreach(以型号VAR项)
{
<立GT;
...
< /李>
}
< / UL>
和它看起来像:
---------
| TEST1 | // ID = 1 PARENTID = 0
| TEST2 | // ID = 2 PARENTID = 1
| TEST3 | // ID = 3 PARENTID = 2
| TEST4 | // ID = 4 PARENTID = 2
| TEST5 | // ID = 5 PARENTID = 0
---------
但我需要它显示为:
---------------
| TEST1 | // ID = 1 PARENTID = 0
| TEST2 | // ID = 2 PARENTID = 1
| TEST3 | // ID = 3 PARENTID = 2
| TEST4 | // ID = 4 PARENTID = 2
| TEST5 | // ID = 5 PARENTID = 0
---------------
我怎样才能做到呢?
您需要考虑使用递归函数。为需要更改以这样的方式,它可以在一个递归函数使用的数据结构。
请参阅我已经创建了一个例子
公共类菜单
{
公众诠释标识{搞定;组; }
公众诠释的ParentId {搞定;组; }
公共字符串名称{;组; }
公开名单<菜单> ChildMenus {搞定;组; }
}
您可以用上面的类模型
而在你看来,你可以创建你的递归函数如下示例:
@model名单< TestNs.Models.Menu>
< DIV>\r
@DisplayMenu(型号)\r
< / DIV>
\r
@helper DisplayMenu(列表<菜单>菜单项)\r
{\r
< UL>\r
@foreach(在菜单项VAR项)\r
{\r
<立GT;\r
@项目名\r
@if(item.ChildMenus = NULL&放大器;!&安培; item.ChildMenus.Any())\r
{\r
@DisplayMenu(item.ChildMenus)\r
}\r
< /李>\r
}\r
< / UL>\r
}
\r
最后,来自你的控制器,你可以prepare你的菜单,并把它发送到您的视图。请使用如下所示的示例:
公众的ActionResult指数()
{
VAR菜单=新的List<菜单>
{
新菜单
{
ID = 1,
的ParentId = 0,
NAME =家,
ChildMenus =新的List<菜单>
{
新菜单
{
ID = 2,
的ParentId = 1,
NAME =商务,
ChildMenus = NULL
},
新菜单
{
ID = 3,
的ParentId = 1,
NAME =社会,
ChildMenus =新的List<菜单>
{
新菜单
{
ID = 4,
的ParentId = 3,
NAME =EDUC
ChildMenus = NULL
},
新菜单
{
ID = 5,
的ParentId = 3,
NAME =奥普,
ChildMenus = NULL
}
},
}
},
}
};
返回查看(菜单);
}
}
把上,看看如何可以达到你想要的。当然,你需要从数据源拉你的数据。
我希望这可以帮助您解决您的问题。
享受!
I have a object list as model on my .cshtml page.
@model IEnumerable<someObject>
Each object have name, id and parentid.
Right now I am showing data as simple list:
<ul>
@foreach (var item in Model)
{
<li>
...
</li>
}
</ul>
and it looks like:
---------
|test1 | //id=1 parentid=0
|test2 | //id=2 parentid=1
|test3 | //id=3 parentid=2
|test4 | //id=4 parentid=2
|test5 | //id=5 parentid=0
---------
BUT I need to show it as:
---------------
|test1 | //id=1 parentid=0
| test2 | //id=2 parentid=1
| test3 | //id=3 parentid=2
| test4 | //id=4 parentid=2
|test5 | //id=5 parentid=0
---------------
How can I accomplish that?
You need to consider using recursive function. For that you need to change your data structure in such a way that it can be used in a recursive function.Please see an example I have created
public class Menu
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public List<Menu> ChildMenus { get; set; }
}
You can use the above class in your modelAnd in your view, you can create your recursive function as the sample below:
@model List<TestNs.Models.Menu>
<div>
@DisplayMenu(Model)
</div>
@helper DisplayMenu(List<Menu> menuItems)
{
<ul>
@foreach (var item in menuItems)
{
<li>
@item.Name
@if (item.ChildMenus != null && item.ChildMenus.Any())
{
@DisplayMenu(item.ChildMenus)
}
</li>
}
</ul>
}
And finally from your controller, you can prepare your menu and call send it to your view. Please use the sample shown below:
public ActionResult Index()
{
var menu = new List<Menu>
{
new Menu
{
Id = 1,
ParentId = 0,
Name = "Home",
ChildMenus = new List<Menu>
{
new Menu
{
Id = 2,
ParentId = 1,
Name = "Business",
ChildMenus = null
},
new Menu
{
Id = 3,
ParentId = 1,
Name = "Social",
ChildMenus = new List<Menu>
{
new Menu
{
Id = 4,
ParentId = 3,
Name = "Educ",
ChildMenus = null
},
new Menu
{
Id = 5,
ParentId = 3,
Name = "opp",
ChildMenus = null
}
},
}
},
}
};
return View(menu);
}
}
Put that on and see how you can achieve what you want. Of course you need to pull your data from your data source. I hope this help you to address your issue.Enjoy!!
这篇关于如何显示在CSHTML IEnumerable的层次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!