我想在博文中输出所有评论。我已成功输入数据库中的所有数据,我可以输出帖子。但是我无法输出评论。
我的帖子模型如下:
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 :(得分: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>
}
}