Recursion in ASP.NET Core Razor views

谁说我不能喝 提交于 2019-12-05 05:04:36

Put the code for rendering a comment inside a partial view, and render it with a call to @Html.Partial("comment", comment).

Then within that comment partial view you'd have something like

@model Comment

Title: @Model.Title
Message: @Model.Message

@if (Model.ChildComments.Any())
{
    <ul>
        @foreach (var childComment in Model.ChildComments)
        {
            <li>
                @Html.Partial("comment", childComment)
            </li>
        }
    </ul>
}

This will render each comment, plus all its children (if any), recursively.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!