MVC3 Razor 中的父子列表
我正在尝试显示嵌套的父\子产品列表。我的表有一个 Id 字段和一个 ParentId 字段。我有清单<>产品对象,现在我想将其显示在视图上。我想出了以下解决方案,但我确信还有更好的解决方案。
我的解决方案:
我知道主要产品\组的名称,因此在循环访问这些名称的数组时,我查询 List<> Product.Name 的产品 == 数组项。一旦找到它,
我就会开始一个无序列表
- Product.Name
通过查询列表查找任何子项<> ParentId == Product.Id 的任何记录的产品数。如果有的话,我几乎会重复这个过程,但这次使用子产品作为父产品并寻找它的子产品。 一旦产品不再有子项,我就会关闭列表项
and then close list
添加我的列表项
尽管这有效,但现在我知道我的列表不会超过三个级别,所以我只编码了这意味着永远不会看到曾祖父的孩子。我想让它能够深入到需要的程度。有什么想法吗?
谢谢
I'm trying to display a nested Parent \ Child list of products. My table has an Id field and a ParentId field. I have the List<> of Product objects and now I want to display it on a view. I came up with the following solution but I'm sure there is a better one.
My solution:
I know the names of the main products \ groups so while looping through an array of those names, I query the List<> of Products for the Product.Name that == array item. Once I find it,
I start an un-ordered list
- Product.Name
Find any child items by querying the List <> of Products for any records who's ParentId == Product.Id. If there are some, I pretty much repeat the process but this time using the Child Product as the Parent Product and looking for it's children.
Once there are no more children for a Product, I close the list item
Add my list item
and then close list
Even though this works, right now I know my list will not be deeper than three levels so I only coded for that meaning that the child of say a Great Grand Dad would never be seen. I would like to make so that it will go deep as it needs to be. Any thoughts?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过这样的事情吗?
在主视图中,渲染没有父级的产品:
在部分视图中,递归渲染后代:
在控制器中,仅返回直接子级:
Have you tried something like this?
In master view, render products with no parent:
In partial view, recursively render offspring:
In controller, return only the direct children: