【问题标题】:how to display the comments under the post如何在帖子下显示评论
【发布时间】:2017-02-11 22:33:01
【问题描述】:

我的项目有问题,我是 MVC 的新手,所以请帮帮我。我在相关帖子下显示 cmets 时遇到问题,就像我们在 Facebook 上发帖一样,我们在帖子下发表评论并显示,我已经显示并列出了所有帖子,在此下我显示了 cmets 字段,我只是想知道如何显示相关评论, 查看

    @foreach (Post item in Model.posts)
{
    foreach(Comment c in item.comments)
    {
<div>

    <p>
        @item.Body
    </p>
    <p>@item.timeDate</p>
    <p>@c.Body</p>

</div>   
    using (Html.BeginForm("CreateComment", "Posts", FormMethod.Post))
    {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Comment</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.comment.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.comment.Name)
            @Html.ValidationMessageFor(model => model.comment.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.comment.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.comment.Email)
            @Html.ValidationMessageFor(model => model.comment.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.comment.Body)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.comment.Body)
            @Html.ValidationMessageFor(model => model.comment.Body)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.comment.dateTime)
        </div>
        <div class="editor-field">
           @Html.TextBoxFor(model => model.comment.dateTime, new { id = "datepicker", @Value = @DateTime.Now })
           @Html.ValidationMessageFor(model => model.post.timeDate)
        </div>       
        <div class="editor-field">
           <input type ="text" hidden="hidden" value="@item.Id" name="txtpostId"/>
            @Html.ValidationMessageFor(model => model.comment.PostId)
        </div>

        <p>
            <input type="submit" value="Create"/>
        </p>
    </fieldset>
    }
  }
}

控制器

 public ActionResult Index()
    {
        objVmPost.comment = new Comment();
        objVmPost.post = new Post();
        List<Post> mylist = db.Posts.Include(post => post.Comments).ToList();
        objVmPost.posts = mylist;
        return View("Index",objVmPost);
    }
 [HttpPost]
    public ActionResult CreateComment(VmPost objVmpost)
    {
        objVmpost.comment.PostId = Convert.ToInt32(Request.Form["txtpostId"]);
        db.Comments.Add(objVmpost.comment);
        db.SaveChanges();
        List<Post> mylist = objPostDb.GetAll().ToList();
        objVmpost.posts = mylist;
        return View("index", objVmpost);
    }

型号

public partial class Comment
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Body { get; set; }
    public System.DateTime dateTime { get; set; }
    public int PostId { get; set; }

    public virtual Post Post { get; set; }
}

public partial class Post
    {
        public Post()
        {
            this.Comments = new HashSet<Comment>();
            this.Tags = new HashSet<Tag>();
        }

        public int Id { get; set; }
        public System.DateTime timeDate { get; set; }
        public string Body { get; set; }

        public virtual ICollection<Comment> Comments { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }
    }

This is the problem click here

【问题讨论】:

  • foreach (Comment c in Model.comments) 显示所有 cmets,而不是与 Post 关联的 cmets 您需要 foreach (Comment c in item.Comments)(并且您的视图模型不需要 comments 的属性 - Post 模型已经包含 Comment 集合的属性
  • 我已经这样做了,但它只显示那些有 cmets 并且显示不正确的帖子,它显示为:: 如果帖子有 2 个 cmets,它会在每个评论中显示帖子两次
  • 它没有! (但你没有展示你的尝试,所以我们无法猜测你犯的错误)
  • 查看视图脚本我已将脚本放入 foreach 循环中我已按照您所说更改了嵌套的 foreach
  • 您现在尝试的代码,您声称它不起作用。但是 Paweł Hemperek 已经解释了你需要怎么做 - 并且根据你对那个正确答案的 cmets,很明显你不明白 - 如果你正确地遵循它,你不可能得到你所声称的,除非你的数据库中的数据不正确。

标签: asp.net asp.net-mvc linq asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

在你的控制器中你正在这样做

List<Comment> cmntlist = db.Comments.ToList();

翻译成SQL

SELECT * FROM `comments`

所以,你不是在检索相关的 cmets,而是所有存储在数据库中的 cmets。

您可以使用延迟加载机制,您的控制器将如下所示

public ActionResult Index()
{
    objVmPost.post = new Post();
    List<Post> mylist = db.Posts.ToList();
    objVmPost.posts = mylist;
    return View("Index",objVmPost);
}

或急切加载

public ActionResult Index()
{
    objVmPost.post = new Post();
    List<Post> mylist = db.Posts.Include(post => post.Comments).ToList();
    objVmPost.posts = mylist;
    return View("Index",objVmPost);
}

你可以阅读更多关于lazy loading and eager loading

无论您想采用哪种方法,您的 foreach 循环都应如下所示:

foreach(var post in Model.Posts){
    <p>@post.Body</p>
    <p>@post.timeDate</p>
    foreach(var comment in post.Comments){
        <p>@comment.Body</p>
    }
    // here you can add your comment form
}

【讨论】:

  • 我也做了同样的事情,但问题是它只显示评论的帖子,另一个问题是它显示帖子的次数与评论一样多,就像一个帖子有两个 cmets,所以帖子显示两次每条评论,
  • @FaisalShahab 另外,它只显示评论过的帖子是什么意思?
  • Hemperek 评论帖子是指人们评论的帖子。
  • 我不想只显示那些被人评论的帖子我想显示所有帖子以及那些被人评论的帖子,就像脸书一样,在 facebook 你可以看到多个用户对单个帖子发表评论并显示帖子下的 cmets 列表
  • 我的视图已经使用 VMPost 进行了强类型化 在 VMPost 中有 4 个属性 public Post post {set; get;} public List post {set;获取;}公众评论评论{设置; get;} public List cmets {set;得到;}
猜你喜欢
  • 2018-10-21
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 2019-04-12
  • 2019-12-07
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多