回复的评论将无法正确显示(ASP.NET MVC)

问题描述 投票:0回答:1

(转贴问题,因为另一个被搁置然后编辑但没有重新打开)

我在我的网站上的评论部分显示评论回复时遇到问题。我已经做了这样有一个原始评论和评论可以有子评论(回复)和我设置我的代码它的方式确实有效,但如果在一个部分有2个原始评论和1个回复,那么它显示尽管我已将其编码为仅显示在特定的原始评论上,但两者都有回复。

评论模型:

   namespace ComicbookWebpage.Models
{
    public class ComicComment
    {
        public int Id { get; set; }
        public string Comment { get; set; }
        public DateTime Posted { get; set; }

        public string UserId { get; set; }
        public virtual ApplicationUser User { get; set; }

        public int ComicId { get; set; }
        public Comic Comic { get; set; }

        public List<SubComicComment> SubComicComments { get; set; }
    }
}

SubComment模型(回复):

    namespace ComicbookWebpage.Models
{
    public class SubComicComment
    {
        public int Id { get; set; }
        public string CommentText { get; set; }
        public DateTime Posted { get; set; }

        public SubComicComment() {

            Posted = DateTime.Now;
        }

        public string UserId { get; set; }
        public ApplicationUser User { get; set; }

        public int ComicId { get; set; }
        public Comic Comic { get; set; }

        public int OriginalCommentId { get; set; }
        public ComicComment ComicComment { get; set; }
    }
}

这是我用于所有数据的视图模型(vm):

namespace ComicbookWebpage.Models.ViewModels
{
    public class ComicVM
    {
        public Comic Comic { get; set; }
        public Series Series { get; set; }

        public List<ComicComment> ComicComments { get; set; }
        public List<SubComicComment> SubComicComments { get; set; }
    }
}

因此,您可以看到我的子评论表中有一个“OriginalCommentId”,以便我可以告诉我的子评论他们所属的原始评论,因此它们只显示在该特定评论下。但问题就像我上面所说的那样,它在同一页面上显示我的子评论下2个不同的原始评论,如果该页面有2个原始评论,这里是一个图像:

(Image) Comments in view (Browser SS)

在每个评论的右侧,您可以看到一个ID,它是评论所具有的ID,您可以清楚地看到ID 9有一个ID为2的子评论,根据我的编码完全错误。因为我告诉我的列表要呈现原始注释id与子注释的OriginalCommentId相同的数据,所以它们都应该具有ID 9,但子注释由于某种原因而具有ID 2 ...

这是控制器代码(查看vm.SubComicComments):

    public ActionResult Comic(int id)
{
    ComicVM vm = new ComicVM();
    vm.Comic = db.Comics.Include(m => m.Series).Where(m => m.Id == id).FirstOrDefault();
    vm.Series = db.Series.FirstOrDefault();
    vm.ComicComments = db.ComicComments.Where(m => m.Comic.Id == id).ToList();
    vm.SubComicComments = db.SubComicComments.Where(m => m.ComicId == id && m.ComicComment.Id == m.OriginalCommentId).ToList();
    db.Users.ToList();

    return View(vm);
}

这是视图代码:

@using Microsoft.AspNet.Identity
@using System.Data.Entity;
@model ComicbookWebpage.Models.ViewModels.ComicVM
@{
    ViewBag.Title = @Model.Comic.Title;
}

<a class="btn btn-default" href="/Series/Details/@Model.Comic.SeriesId"><i class="glyphicon glyphicon-menu-left"></i> Back</a>
<hr />
<h5><b>Title:</b> @Model.Comic.Title</h5>
<h5><b>Series:</b> @Model.Comic.Series.Title</h5>
<h5><b>Pages:</b> @Model.Comic.PageAmount</h5>
<hr />


<h4><i class="glyphicon glyphicon-comment"></i> Leave a comment:</h4>

<br />
@if (User.Identity.IsAuthenticated)
{
    <div class="col-sm-1">
        <div class="thumbnail">
            <img class="img-responsive user-photo" src="https://ssl.gstatic.com/accounts/ui/avatar_2x.png">
        </div><!-- /thumbnail -->
    </div><!-- /col-sm-1 -->

    <div class="col-sm-5">

        <form action="/Series/Comic/@Model.Comic.Id" method="post">
            <input type="hidden" name="Posted" value="@DateTime.Now" />
            <input type="hidden" name="UserId" value="@User.Identity.GetUserId()" required />
            <input type="hidden" name="ComicId" value="@Model.Comic.Id" />
            <textarea class="form-control form-text" type="text" name="Comment" placeholder="Type your comment..." required></textarea>
            <br />
            <button type="submit" class="btn bg-dark">Send</button>
        </form>
    </div><!-- /col-sm-5 -->
}
else
{
    <h5>You have to be logged in to post a comment.</h5>
    <p><a href="/Account/Login">Click here to login</a></p>
}
<div class="row">
    <div class="col-md-12">
        @if (Model.ComicComments.Count > 0)
        {
            <h4>(@Model.ComicComments.Count) Comments:</h4>
        }
        else
        {
            <h4>0 Comments:</h4>
            <p>There are currently no comments posted on this comic book.</p>
        }
    </div>
</div>
@foreach (var Comment in Model.ComicComments.Where(m => m.ComicId == m.Comic.Id))
{
    <div class="comments-container">
        <ul id="comments-list" class="comments-list">
            <li>
                <div class="comment-main-level">
                    <!-- Avatar -->
                    <div class="comment-avatar"><img src="https://i9.photobucket.com/albums/a88/creaticode/avatar_1_zps8e1c80cd.jpg" alt=""></div>
                    <!-- Contenedor del Comentario -->
                    <div class="comment-box">
                        <div class="comment-head">
                            <h6 class="comment-name by-author">@Comment.User.UserName</h6>
                            <span>posted on @Comment.Posted.ToShortDateString()</span><i>ID: @Comment.Id</i>
                        </div>
                        <div class="comment-content">
                            @Comment.Comment
                        </div>
                    </div>
                </div>
                <!-- Respuestas de los comentarios -->
                <ul class="comments-list reply-list">
                    @if (Model.SubComicComments.Count > 0)
                    {
                        foreach (var SubComment in Model.SubComicComments.Where(m => m.OriginalCommentId == m.ComicComment.Id))
                        {
                            <li>
                                <!-- Avatar -->
                                <div class="comment-avatar"><img src="https://i9.photobucket.com/albums/a88/creaticode/avatar_2_zps7de12f8b.jpg" alt=""></div>
                                <!-- Contenedor del Comentario -->
                                <div class="comment-box">
                                    <div class="comment-head">
                                        <h6 class="comment-name">@SubComment.User.UserName</h6>
                                        <span>posted on @SubComment.Posted.ToShortDateString()</span><i>ID: @SubComment.OriginalCommentId</i>
                                    </div>
                                    <div class="comment-content">
                                        @SubComment.CommentText
                                    </div>
                                </div>
                            </li>
                        }
                    }
                </ul>
            </li>
        </ul>
    </div>
}

如果你们能在这里找出问题,我会很感激。对我而言,代码是非常符合逻辑的,应该可行,但事实并非如此,而且我已经尝试了很多东西,但没有运气。

先感谢您。

asp.net-mvc entity-framework
1个回答
1
投票

对于您的SubComments foreach声明:

foreach (var SubComment in Model.SubComicComments.Where(m => m.OriginalCommentId == m.ComicComment.Id))

应该:

foreach (var SubComment in Model.SubComicComments.Where(m => m.OriginalCommentId == Comment.Id))

没有?您想要在封闭的Comments迭代中声明的Comment变量中检查SubComment.OriginalCommentId。

顺便说一句,在你的第一个foreach语句中,我不认为where子句正在做任何事情:

@foreach (var Comment in Model.ComicComments.Where(m => m.ComicId == m.Comic.Id))

只要您的包含已加载,ComicID == Comid.Id应始终为true

© www.soinside.com 2019 - 2024. All rights reserved.