2014-05-18 42 views
0

我想输出博客帖子中的所有评论。我已成功输入数据库中的所有数据,并可输出帖子。但是我无法输出评论。无法输出帖子中的所有评论

我的帖子模型如下:

public class Post 
    { 
     [Key] 
     public int PostId { get; set; } 
     public string Title { get; set; } 
     public DateTime CreatedDate{get;set;} 
     public DateTime UpdateDate { get; set; } 
     public string Body { get; set; } 
     public ICollection<Comment> Comments { get; set;} 

    } 

我的评论模型如下:

public int CommentId { get; set; } 
public int PostId { get; set; } 
[ForeignKey("PostId")] 
public virtual Post Post{get; set;} 
public string CommentCreateDate { get; set; } 
public string CommentUpdateDate { get; set; } 
public string CommeterName { get; set; } 
public string EmailAddress { get; set; } 
public string CommentText { get; set; } 
public bool Approved { get; set; } 

我的操作如下:

[HttpPost] 
public ActionResult CreateComment(int ?id, CommentViewModel model) 
{ 
    Post post = GetPost(id); 
    var comment = new Comment(); 
    comment.Post = post; 
    comment.CommentCreateDate = DateTime.Now.ToString(); 
    comment.CommeterName = model.Name; 
    comment.EmailAddress = model.Email; 
    comment.CommentText = model.Text; 

    db.Comments.Add(comment); 
    db.SaveChanges(); 

    m_commentList.Add(m_comment); 

    ViewData["Comment"] = comment; 
    ViewBag.Comment = comment; 
    TempData["Comment"] = comment; 

    return RedirectToAction("Index"); 

} 

我的索引视图被如下:

@using Blog.Models 

@{ 


    ViewBag.Title = "Index"; 
    var viewDataComment = ViewData["Comment"] as Comment; 
    var tempDataComment = TempData["Comment"] as Comment; 


    bool isPreviousLinkVisible = ViewBag.IsPreviousLinkVisible ?? false; 
    bool isNextLinkVisible = ViewBag.IsNextLinkVisible ?? false; 
    bool isAdmin = ViewBag.IsAdmin ?? false; 
} 

@foreach (Post post in Model) 
    { 


     <div class="newsResult"> 

      <div class="title">@Html.DisplayFor(modelItem => post.Title)</div> 
      <div class="updated"> <b>Created:</b> @Html.DisplayFor(modelItem => post.CreatedDate) </div> 
      <div class="updated"> <b>Updated:</b> @Html.DisplayFor(modelItem => post.UpdateDate)</div> 
      <div class="data"> <b></b> @Html.DisplayFor(modelItem => post.Body)</div> 


      @*<div class="data"> <b></b> @Html.DisplayFor(x => tempDataComment.CommentText)</div>*@ 

      @foreach (Comment comment in post.Comments.OrderBy(x => x.CommentCreateDate)) //Offending line 
      { 
       <div class="comment"> 
        <div class="commentName"> 
         @if (!string.IsNullOrWhiteSpace(comment.EmailAddress)) 
         { 
          <a href="mailto: @comment.EmailAddress;">@comment.CommeterName</a> 
         } 
         else 
         { 
          @comment.CommeterName; 
         } 
        </div> 
        said: 
        <div class="commentBody">@Html.Raw(Html.Encode(comment.CommentText).Replace("\n", "<br/>"))</div> 
        <div class="commentTime">at @comment.CommentCreateDate.ToString() on @comment.CommentCreateDate.ToString()</div> 
       </div> 
      } 

上面的帖子显示没有问题。但是,当我试图访问它给了我一个异常的评论(出错行是第二的foreach的开始):

System.ArgumentNullException: Value cannot be null 

我确实知道该数据已被创建并包括注释这是否则可用于var tempDataComment

这是怎么发生的?任何人都可以帮忙吗?

预先感谢您!

+0

这是发生在后或得到?你的帖子只是返回索引? – Peru

+0

@Peru它发生在帖子中。在上面的视图的职位控制器的索引视图。我想列出在该视图中的所有评论。 – bluetxxth

+0

检查您传递给控制器​​的型号是否有价值。我猜这是作为NULL – Peru

回答

0

的问题是固定的,加入支票无效:

@if (post.Comments != null) //added this 
      { 
      foreach (Comment comment in post.Comments.OrderBy(x => x.CommentCreateDate)) 
      { 

      <div class="comment"> 
       <div class="commentName"> 
        @if (!string.IsNullOrWhiteSpace(comment.EmailAddress)) 
        { 
         <a href="mailto: @comment.EmailAddress;">@comment.CommeterName</a> 
        } 
        else 
        { 
         @comment.CommeterName; 
        } 
       </div> 
       said: 
       <div class="commentBody">@Html.Raw(Html.Encode(comment.CommentText).Replace("\n", "<br/>"))</div> 
       <div class="commentTime">at @comment.CommentCreateDate.ToString() on @comment.CommentCreateDate.ToString()</div> 
      </div> 
      } 
     } 
相关问题