2016-10-03 154 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_? –

相关问题