获取当前评论ID以对该评论发表评论?

时间:2016-10-26 20:04:22

标签: c# asp.net-mvc asp.net-mvc-5

我有一个MVC5项目,我们正在创建一个IMDB样式克隆。它适用于游戏,电影和书籍,而不仅仅是电影。

该页面包含Review Feed,您可以在其中显示所有评论的列表,还可以添加新评论。

当我点击评论时,我会看到评论详情页面(〜Views / Reviews / Details.cshtml),到目前为止一切顺利。它显示了所有细节,没有问题。

在评论/详细信息视图中,我想添加评论部分。所以我把它添加到了我的Review / Details.cshtml页面:

<h4>Comments</h4>

@{
    Html.RenderPartial("_CommentPartial");
}

@foreach (var comment in Model.CommentToReviews)
{
    <div class="row">
        <div class="col-xs-2">
            @Html.Raw(comment.User.Username)
            <br /> 
            posted at: @Html.Raw(comment.CreatedDate.ToShortDateString())
        </div>
        <div class="col-xs-10">
            @Html.Raw(comment.Comment)
        </div>
    </div>
}

问题在于,我不确定如何将当前的审核ID传递给创建注释部分视图。

这是部分:

@model gmbdb.CommentToReview

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Add a comment</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

这是CommentToReviewsController中的Create get / post方法,

得到:

public ActionResult Create(Guid reviewId)
{
      var newComment = new CommentToReview();
      newComment.UserId = ((User) Session["currentUser"]).Id;
      newComment.ReviewId = reviewId;
      return View(newComment);
}

发布:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,UserId,ReviewId,Comment,CreatedDate")] CommentToReview commentToReview)
        {
            if (ModelState.IsValid)
            {
                commentToReview.Id = Guid.NewGuid();
                commentToReview.CreatedDate = DateTime.Now;

                //find current Review and add this comment to that review
                foreach (var review in db.Reviews)
                {
                    if (review.Id == commentToReview.ReviewId)
                    {
                        review.CommentToReviews.Add(commentToReview);

                        db.SaveChanges();
                    }
                }

                return RedirectToAction("Index");
            }

            ViewBag.ReviewId = new SelectList(db.Reviews, "Id", "Title", commentToReview.ReviewId);
            ViewBag.UserId = new SelectList(db.Users, "Id", "Username", commentToReview.UserId);
            return View(commentToReview);
        }

1 个答案:

答案 0 :(得分:0)

更改“详情”视图以调用操作而非部分操作,以便您能够收到当前评论的ID:

<强>查看/评论/ Details.cshtml

<h4>Comments</h4>
@{
    Html.RenderAction("Create", new { reviewId = Model.Id });
}

按如下方式创建操作:

public ActionResult Create(Guid reviewId)
{
    var newComment = new CommentToReview
    {
        UserId = UserId,
        ReviewId = reviewId
    };

    return PartialView("_CommentPartial", newComment); //return the partial from the action
}

现在,您的创建操作会收到审核的ID,您可以将其与模型关联并返回_CommentPartial局部视图。

现在唯一需要注意的事情就是在你的Post动作中你不能做任何重定向,因为它现在是一个子动作,所以你可以将JsonResult返回到视图并通过ajax调用并添加注释而不刷新你的页面。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,UserId,ReviewId,Comment,CreatedDate")] CommentToReview commentToReview)
{
    if (ModelState.IsValid)
    {
        //the code to add the comment

        return Json(new
        {
            success = true,
            //other data for the comment, maybe the rendered comment as html to display it on the view.
        });
    }

    ViewBag.ReviewId = new SelectList(db.Reviews, "Id", "Title", commentToReview.ReviewId);
    ViewBag.UserId = new SelectList(db.Users, "Id", "Username", commentToReview.UserId);
    return View(commentToReview);
}

希望这有帮助!