Skip to main content
 首页 » 编程设计

c#之在 asp.net mvc 中创建多级评论系统

2024年02月06日14txw1958

我目前正在写一个用 asp.net mvc 编写的博客。我有一张表,用以下模型存储评论

public partial class Comment 
    { 
        public int CommentId { get; set; } 
        public string Comment { get; set; } 
        public System.DateTime CommentDate { get; set; } 
        public int AuthorId { get; set; } 
        public int PostId { get; set; } 
        public int CategoryId { get; set; } 
 
        public virtual Category Category { get; set; } 
        public virtual Post Post { get; set; } 
        public virtual UserProfile UserProfile { get; set; } 
    }  

目前评论仍然是一级评论,即用户不能回复评论。我怎样才能实现一个多级评论系统,使评论可以有回复,回复可以有另一个回复等等。

请您参考如下方法:

添加引用 Comments 表的 parentCommentId 列。 之后在每个回复中:

  1. 如果是直接回复帖子,请将 parentCommentId 列留空
  2. 如果是对先前发表的评论的回复,请将该评论 ID 放入此列。在这种情况下,您可以将 postid 列留空或不留空。这取决于你的喜好!