下面的代码(在控制器中)读取上一页中的特定行数据(通过视图中的id
获取@Html.ActionLink("Details", "Details", new { id=item.Id })
)
public ActionResult Details(Guid? id) {
if (id == null) {
return Content("id = null..");
}
Review review = db.Reviews.Find(id);
if (review == null)
{
return Content("review = null.. ");
}
return View(review);
}
到目前为止一直很好,但现在我想让访问者/用户留下评论,给予喜欢/不喜欢等等。我想这是View的详细信息/创建(换句话说,读取/插入)模板的组合?我应该在控制器中做些什么来完成这项工作?
从现在开始,我不知道该做什么,因为我是MVC的新手。
这就是我的数据库的样子:(从EF-model(数据库优先)中挑选):
用户:
public System.Guid Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
评论:(作者创建评论的地方)
public System.Guid Id { get; set; }
public System.Guid CreatorUserId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public System.DateTime CreatedDate { get; set; }
public int UserRating { get; set; }
public int LikeCount { get; set; }
public int DislikeCount { get; set; }
评论评论:(评论从其他用户那里获得评论)
public System.Guid Id { get; set; }
public System.Guid UserId { get; set; }
public System.Guid ReviewId { get; set; }
public string Comment { get; set; }
public System.DateTime CreatedDate { get; set; }
UserReview :(如果用户已经喜欢评论,请避免多次评论)
public System.Guid Id { get; set; }
public System.Guid UserId { get; set; }
public System.Guid ReviewId { get; set; }
public bool HasLiked { get; set; }
这是显示所选行详细信息的视图,如下所示:
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
@Html.DisplayFor(model => model.Description)
</dd>
@* ... and so on. Not sure how to add "create"-inputs for e.g. comments etc*@
我正在使用Session["LoggedUserID"]
获取当前用户的Id
。
答案 0 :(得分:2)
首先,为了配置它们之间的关系,稍微改变你的实体:
public class User
{
[Key]
public System.Guid Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public virtual ICollection<CommentToReview> Comments { get; set; }
public virtual ICollection<UserToReview> Reviews { get; set; }
}
public class Review
{
[Key]
public System.Guid Id { get; set; }
public System.Guid CreatorUserId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public System.DateTime CreatedDate { get; set; }
public int UserRating { get; set; }
public int LikeCount { get; set; }
public int DislikeCount { get; set; }
public virtual ICollection<CommentToReview> Comments { get; set; }
public virtual ICollection<UserToReview> Users { get; set; }
}
public class CommentToReview
{
[Key]
public System.Guid Id { get; set; }
[ForeignKey("User")]
public System.Guid UserId { get; set; }
public virtual User User { get; set; }
[ForeignKey("Review")]
public System.Guid ReviewId { get; set; }
public virtual Review Review { get; set; }
public string Comment { get; set; }
public System.DateTime CreatedDate { get; set; }
}
public class UserToReview
{
[Key]
public System.Guid Id { get; set; }
[ForeignKey("User")]
public System.Guid UserId { get; set; }
public virtual User User { get; set; }
[ForeignKey("Review")]
public System.Guid ReviewId { get; set; }
public virtual Review Review { get; set; }
public bool HasLiked { get; set; }
}
现在转到ReviewsController并添加此操作,为其添加新注释:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateComment([Bind(Include = "Id,UserId,ReviewId,Comment,CreatedDate")] CommentToReview commentToReview)
{
if (ModelState.IsValid)
{
commentToReview.Id = Guid.NewGuid();
commentToReview.UserId = Session["LoggedUserID"].ToString();
commentToReview.CreatedDate = DateTime.Now;
db.CommentToReviews.Add(commentToReview);
db.SaveChanges();
return RedirectToAction("Details", "Reviews", new { id = commentToReview.ReviewId });
}
return RedirectToAction("Details", "Reviews", new { id = commentToReview.ReviewId });
}
然后你应该在Views / Review Folder下的PartialViews下创建:
_CreateCommentPartial.cshtml:用于在同一个详细信息页面中创建评论
@using (Html.BeginForm("CreatePartial", "Reviews"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CommentToReview</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model=> model.ReviewId)
<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>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
_CommentDetailPartial.cshtml:用于显示现有评论详情
@model MvcApp.Models.CommentToReview
<div class="panel panel-default">
<div class="panel-heading">@Model.User.Email</div>
<div class="panel-body">
@Model.Comment
</div>
<div class="panel-footer">@Model.CreatedDate.ToString()
</div>
</div>
最后修改Review(Views / Reviews / Details.cshtml)的详细信息视图
@model MvcApp.Models.Review
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Review</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
@Html.DisplayFor(model => model.Description)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CreatedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CreatedDate)
</dd>
</dl>
<h6>Comments</h6>
<hr />
@foreach(var comment in Model.Comments)
{
Html.RenderPartial("_CommentDetailsPartial.cshtml", comment);
}
<br />
@{
var newComment = new MvcApp.Models.CommentToReview { ReviewId = Model.Id };
Html.RenderPartial("_CreateComment", newComment);
}
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>
请注意,为简单起见,我在Review Controller和View下创建了所有PartialViews和操作。而且当你添加一个新的评论时,它会刷新页面,也许更好的是你使用ajax来提高性能。