1

我有这样的评论模型:

public class Comment
    {
        public int? ParentId { get; set; }
        public string Text { get; set; }
        public int ProjectId { get; set; }
        public int UserWhoTypeId { get; set; }
    }

我想通过 parentID 在另一个下显示评论。父评论将出现在div,子评论将出现在

<ul>
   <li>
      child comments go here
   </li>
</ul>

例如,

  <ul>
       <li>
          <div>
               parent comments go here
          </div>
          <ul>
             <li>
                 child comments go here
             </li>
           </ul>
       </li>
  </ul>

我首先需要使用像树一样的 LINQ 收集评论,然后像上面显示的那样应用它。请提供任何链接或建议。

编辑:

我创建模型为

public class CommentListModel
{
    public Comment Comment{ get; set; }
    public List<Comment> Childs { get; set; }
}

我收集了 1 个列表中的所有评论:

List<CommentListModel>  CommentHierarchy = MyService.GetCommentHierarchy();

现在,我需要像树层次结构一样在视图中显示 CommentHierarchy。请帮忙。

4

1 回答 1

2

您可以将 CommentListModel 的“Childs”属性更改为 CommentListModel 的集合,如下所示:

public class CommentListModel
{
    public Comment Comment { get; set; }
    public List<CommentListModel> Childs { get; set; }
}

为 CommentListModel 创建一个局部视图作为显示模板(将文件放在 DisplayTemplates 文件夹下):

@model CommentListModel
<ul>
    <li>
        <div>@Html.DisplayFor(m => m.Comment.Text)</div>
        @Html.DisplayFor(m => m.Childs)
    </li>
</ul>

然后在您的父视图中,只需调用:

 @Html.DisplayFor(m => m)

假设父视图的模型是 CommentListModel 对象的集合。

这将允许您的列表尽可能深入地递归您的收藏。

于 2013-01-18T16:03:58.293 回答