如何在同一视图中显示'细节'和'创建'? MVC
问题描述:
下面的代码(控制器)从上页读取特定行的数据(通过@Html.ActionLink("Details", "Details", new { id=item.Id })
得到id
查看里面)如何在同一视图中显示'细节'和'创建'? MVC
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模型(数据库一)挑选):
用户:
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; }
CommentReview:(凡检讨会从其他用户的评论)
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"]
吨o为当前用户获取Id
。
答
首先改变你的实体一点点为他们之间的配置关系:
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文件夹下创建部分视图下方:
_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>
末修改详细查看评论(查看/评论/ 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>
只知道为了简单起见,我在“审阅控制器”和“视图”下创建了所有部分视图和操作。并且当你添加一个新的评论时,它会刷新页面,也许你会更好地使用ajax来获得更好的性能。
你可以使用局部视图http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC – DanielVorph
啊,我明白了。只有一个问题,我如何分享数据,可以说部分视图的“Id”?例如。我想在'review'(view1)中显示一个特定的行,并且我想'CommentToReview'(view2)使用'review'(view1)使用的相同的'Id'。 – skylake
您可以使用该视图的模型,例如在partialview中使用'@ Html.ActionLink(“CommentToReview”,“Details”,new {id = Model.Id})'发送该模型的该特定'Id' – Vijai