【问题标题】:ASP.NET MVC recursionASP.NET MVC 递归
【发布时间】:2022-03-09 19:09:03
【问题描述】:

我正在使用具有如下模型实现递归的 .NET MVC:

    public class Comment
{
    [Key]
    [Required]
    public int CommentId { get; set; }

    [Required]
    public string Content { get; set; }
    public bool Anonymous { get; set; }
    [Required]
    public DateTime Created_date { get; set; }

    [Required]
    public DateTime Last_modified { get; set; }
    public virtual Comment Reply { get; set; }
    public virtual ICollection<Comment> Replys { get; set;}
    public virtual Idea Idea { get; set; }
}

在说明中,每个idea里面都包含了不同的Comment,每个comment还有几个较小的Comment来回复前一个。但是,我不知道如何进行递归以获取评论的回复和每个回复的较小回复,直到控制器中的最后一个回复以及如何在视图中显示它们。由于我的解释有时不清楚,请随时问我任何问题。

  public ActionResult Index(int? i)
    {
        List<Idea> Ideas = db.Ideas.ToList();
        foreach(Idea idea in Ideas)
        {
            idea.Comments = db.Comments.Include(x => x.Reply).ToList();
            idea.Comments=idea.Comments.Where(x => x.Idea == idea).ToList();
            foreach (Comment comment in idea.Comments)
            {
                comment.Replys = GetComments(comment.CommentId); //This function use to get list of reply for comment
                foreach (Comment reply in comment.Replys)
                {
                    reply.Replys=GetComments(reply.CommentId);
                    foreach (Comment reply2 in reply.Replys)
                    {
                        reply2.Replys=GetComments(reply2.CommentId);
                        foreach(Comment reply3 in reply2.Replys)
                        {
                            reply3.Replys = GetComments(reply3.CommentId);
                            //When would this stop ?
                        }
                    }
                }
            }
        }
        return View(Ideas.ToPagedList(i ?? 1, 5));
      
    }

【问题讨论】:

  • 你已经用这个public virtual ICollection&lt;Comment&gt; Replys做对了。您可能只需要添加一个可为空的 int? ReferenceCommentId
  • 我已经在控制器中添加了我的问题,以便您更容易理解

标签: c# asp.net-mvc recursion


【解决方案1】:

使用这个循环;

foreach(Idea idea in Ideas)
{
    idea.Comments = db.Comments.Include(x => x.Reply).ToList();
    idea.Comments= idea.Comments.Where(x => x.Idea == idea).ToList();
    
    foreach (Comment comment in idea.Comments)
    {
        RecursionGetComments(comment);
    }
}

添加此功能;

public void RecursionGetComments(Comment comment){
    comment.Replys = GetComments(comment.CommentId);

    foreach(var reply in comment.Replys){
        RecursionGetComments(reply);
    }
}

如果上面的RecursionGetComments 没有正确填写,你可能需要使用ref 关键字来传递类的当前实例(通过引用传递);

public void RecursionGetComments(ref Comment comment){
    comment.Replys = GetComments(comment.CommentId);

    foreach(var reply in comment.Replys){
        RecursionGetComments(reply);
    }
}

我还注意到,在您的模型中,您使用了关键字virtual

Virtual 通知实体框架导航属性将自动加载/延迟加载,因为您是手动加载 cmets,您可以将其删除。

【讨论】:

  • 非常感谢,这解决了我的问题。我还有一个问题是如何在再次发送到 View 后显示所有 cmets?我是否必须再次进行递归以及如何进行?
  • @helijacky 是的,你也需要使用递归。这是一个很好的答案; stackoverflow.com/a/6423891/5640737
  • 解决了我的整个问题,再次感谢
  • @helijacky,不客气!
猜你喜欢
  • 2015-07-18
  • 2015-04-03
  • 2011-09-19
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
相关资源
最近更新 更多