在asp.net mvc中创建一个多级注释系统

时间:2014-12-30 17:43:10

标签: c# asp.net-mvc

我目前正在开发一个用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; }
    } 

目前,评论仍然是一级评论,即用户无法回复评论。如何以一种评论可以有回复的方式实现多级评论系统,并且该回复可以有另一个回复等等。

4 个答案:

答案 0 :(得分:4)

添加 parentCommentId 列,该列引用“注释”表。 在每次回复之后:

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

答案 1 :(得分:0)

首先我需要原谅你的英语不好,如果你想要一个评论树,因为我理解你评论和回复,你需要回复我的回复,这使你应该再添加1个字段,你可以称之为子评论id它将作为评论表"的一个外键。自我关系"并且此子comcomid将保留父评论ID

答案 2 :(得分:0)

但基本上......

我不知道你的POST类是如何定义的。它是一个集合吗?还是一个简单的课程?如果它不是你想要的集合那么......

在评论类中更改..

public virtual Post Post { get; set; }

为:

Public virtual ICollection<Post> Posts { get; set; } 

或在另一个私有字段中定义..

这将定义关系,也将是您的导航属性..

所以在你的控制器中你可以像这样返回两个表......

public ActionResult GetComments(int id) { db.context.comments.Include("Posts").Where(c => c.CommentID == id).ToList(); }

答案 3 :(得分:0)

您需要创建自定义Htmlhelper / TreeView并将数据传递给此帮助程序。数据模型将根据Mahmoud Moravej。