2016-10-03 197 views
-1

我在我的項目中有問題我是新的MVC所以plz幫助我。我有問題在相關的帖子下顯示評論,就像我們已經發表在臉書上,我們只是在帖子下面發表評論,它顯示,我已經顯示和列出所有帖子,並在此下我已經顯示評論欄,我只是想知道相關的註釋如何顯示, 查看如何顯示帖子下的評論

@foreach (Post item in Model.posts) 
{ 
    foreach(Comment c in item.comments) 
    { 
<div> 

    <p> 
     @item.Body 
    </p> 
    <p>@item.timeDate</p> 
    <p>@c.Body</p> 

</div> 
    using (Html.BeginForm("CreateComment", "Posts", FormMethod.Post)) 
    { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Comment</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.comment.Name) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.comment.Name) 
      @Html.ValidationMessageFor(model => model.comment.Name) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.comment.Email) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.comment.Email) 
      @Html.ValidationMessageFor(model => model.comment.Email) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.comment.Body) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.comment.Body) 
      @Html.ValidationMessageFor(model => model.comment.Body) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.comment.dateTime) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.comment.dateTime, new { id = "datepicker", @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.post.timeDate) 
     </div>  
     <div class="editor-field"> 
      <input type ="text" hidden="hidden" value="@item.Id" name="txtpostId"/> 
      @Html.ValidationMessageFor(model => model.comment.PostId) 
     </div> 

     <p> 
      <input type="submit" value="Create"/> 
     </p> 
    </fieldset> 
    } 
    } 
} 

控制器

public ActionResult Index() 
    { 
     objVmPost.comment = new Comment(); 
     objVmPost.post = new Post(); 
     List<Post> mylist = db.Posts.Include(post => post.Comments).ToList(); 
     objVmPost.posts = mylist; 
     return View("Index",objVmPost); 
    } 
[HttpPost] 
    public ActionResult CreateComment(VmPost objVmpost) 
    { 
     objVmpost.comment.PostId = Convert.ToInt32(Request.Form["txtpostId"]); 
     db.Comments.Add(objVmpost.comment); 
     db.SaveChanges(); 
     List<Post> mylist = objPostDb.GetAll().ToList(); 
     objVmpost.posts = mylist; 
     return View("index", objVmpost); 
    } 

模式

public partial class Comment 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Body { get; set; } 
    public System.DateTime dateTime { get; set; } 
    public int PostId { get; set; } 

    public virtual Post Post { get; set; } 
} 

public partial class Post 
    { 
     public Post() 
     { 
      this.Comments = new HashSet<Comment>(); 
      this.Tags = new HashSet<Tag>(); 
     } 

     public int Id { get; set; } 
     public System.DateTime timeDate { get; set; } 
     public string Body { get; set; } 

     public virtual ICollection<Comment> Comments { get; set; } 
     public virtual ICollection<Tag> Tags { get; set; } 
    } 

This is the problem click here

+2

'foreach(Model.comments中的Comment c)'顯示所有註釋,而不是與'Post'關聯的註釋您需要'foreach(item.Comments中的註釋c)'(並且您的視圖模型不需要'comment'的屬性 - 'Post'模型已經包含一個屬性,它的collecton的Comment * –

+0

我已經這樣做了,但它只顯示那些有評論和顯示不正確的帖子,它顯示爲::如果帖子有2條評論,它顯示帖子兩次,每條評論 –

+0

它不! (但你沒有顯示你所嘗試的,所以我們不能猜測你所犯的錯誤) –

回答

2

在你的控制器,你這樣做是

List<Comment> cmntlist = db.Comments.ToList(); 

其轉換成SQL

SELECT * FROM `comments` 

這樣,你是不是retreiving相關評論,但所有評論存儲在數據庫中。

你可以使用延遲加載機制,你的控制器看起來像這樣

public ActionResult Index() 
{ 
    objVmPost.post = new Post(); 
    List<Post> mylist = db.Posts.ToList(); 
    objVmPost.posts = mylist; 
    return View("Index",objVmPost); 
} 

或預先加載

public ActionResult Index() 
{ 
    objVmPost.post = new Post(); 
    List<Post> mylist = db.Posts.Include(post => post.Comments).ToList(); 
    objVmPost.posts = mylist; 
    return View("Index",objVmPost); 
} 

你可以閱讀更多關於lazy loading and eager loading

無論哪種方法你會喜歡採取,你的foreach循環應該看起來像這樣:

foreach(var post in Model.Posts){ 
    <p>@post.Body</p> 
    <p>@post.timeDate</p> 
    foreach(var comment in post.Comments){ 
     <p>@comment.Body</p> 
    } 
    // here you can add your comment form 
} 
+0

我做了同樣的事情,但問題是它只顯示評論的帖子,另一個問題是它顯示的帖子多次作爲評論,就像一個帖子有兩個評論,所以帖子顯示兩次每個評論, –

+0

@FaisalShahab看看更新後的答案 –

+0

@FaisalShahab另外,你是什麼意思_it只顯示評論post_? –

相關問題