Home > database >  ASP.NET MVC recursion
ASP.NET MVC recursion

Time:03-10

I am working with .NET MVC that have a model implement recursion as below:

    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; }
}

In explanation, each Idea contains different Comments inside it, and each comment also has several smaller Comments that reply to the previous one. However, I have no idea how to do recursion to get replies for a comment and smaller reply for each reply until the last one in controller and also how to display them in view. Feel free to ask me any question since my explanation is not clear sometimes.

  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));
      
    }

CodePudding user response:

Use this loop;

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);
    }
}

Add this function;

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

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

If the RecursionGetComments above didn't fill properly, you might need to use the ref keyword which passes the current instance of the class (pass by reference);

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

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

I also noticed that in your model, you are using the keyword virtual.

Virtual signals entity framework that the navigation property will be loaded automatically / lazy loading, since you are loading your comments manually, you could remove that.

  • Related