MVC3 Razor 中的父子列表

发布于 2025-01-07 14:06:21 字数 532 浏览 2 评论 0原文

我正在尝试显示嵌套的父\子产品列表。我的表有一个 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

    Add my list item

  • 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

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2025-01-14 14:06:21

你尝试过这样的事情吗?

在主视图中,渲染没有父级的产品:

@Html.Action("DisplayChildren", "ControllerName", new { parentId = null })

在部分视图中,递归渲染后代:

@model IEnumerable<Product>
<ul>
    @foreach (var child in Model)
    {
        <li>
            @Html.DisplayFor(m => child.Name)
            @Html.Action("DisplayChildren", "ControllerName", 
                new { parentId = child.Id })
        </li>
    }
</ul>

在控制器中,仅返回直接子级:

public ActionResult DisplayChildren(int? parentId)
{
    var children = GetDirectChildrenOf(parentId);
    if (children.Count() > 0)
        return PartialView(children);
    else return new EmptyResult();
}

Have you tried something like this?

In master view, render products with no parent:

@Html.Action("DisplayChildren", "ControllerName", new { parentId = null })

In partial view, recursively render offspring:

@model IEnumerable<Product>
<ul>
    @foreach (var child in Model)
    {
        <li>
            @Html.DisplayFor(m => child.Name)
            @Html.Action("DisplayChildren", "ControllerName", 
                new { parentId = child.Id })
        </li>
    }
</ul>

In controller, return only the direct children:

public ActionResult DisplayChildren(int? parentId)
{
    var children = GetDirectChildrenOf(parentId);
    if (children.Count() > 0)
        return PartialView(children);
    else return new EmptyResult();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文