2016-09-27 96 views
1

下面的代碼(控制器)從上頁讀取特定行的數據(通過@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

+0

你可以使用局部視圖http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC – DanielVorph

+0

啊,我明白了。只有一個問題,我如何分享數據,可以說部分視圖的「Id」?例如。我想在'review'(view1)中顯示一個特定的行,並且我想'CommentToReview'(view2)使用'review'(view1)使用的相同的'Id'。 – skylake

+0

您可以使用該視圖的模型,例如在partialview中使用'@ Html.ActionLink(「CommentToReview」,「Details」,new {id = Model.Id})'發送該模型的該特定'Id' – Vijai

回答

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文件夾下創建部分視圖下方:

_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來獲得更好的性能。

相關問題