Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在帖子中输出所有评论_C#_Asp.net Mvc_Entity - Fatal编程技术网

C# 无法在帖子中输出所有评论

C# 无法在帖子中输出所有评论,c#,asp.net-mvc,entity,C#,Asp.net Mvc,Entity,我想在一篇博客文章中输出所有评论。我已经成功地在数据库中输入了所有数据,我可以输出帖子。但是,我无法输出评论 我的帖子模式如下: public class Post { [Key] public int PostId { get; set; } public string Title { get; set; } public DateTime CreatedDate{get;set;} public Da

我想在一篇博客文章中输出所有评论。我已经成功地在数据库中输入了所有数据,我可以输出帖子。但是,我无法输出评论

我的帖子模式如下:

  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>
            }
我的行动如下:

  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>
            }
我的索引视图如下:

  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>
            }
我确信数据已经创建,其中包括在var
tempDataComment
中可用的注释

为什么会这样?有人能帮忙吗


提前谢谢你

通过添加空值检查修复了该问题:

@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>
            }
       }
@if(post.Comments!=null)//添加了这个
{
foreach(post.Comments.OrderBy(x=>x.CommentCreateDate)中的注释)
{
@如果(!string.IsNullOrWhiteSpace(comment.EmailAddress))
{
}
其他的
{
@CommeterName;
}
说:
@Html.Raw(Html.Encode(comment.CommentText).Replace(“\n”,“
”)) 在@comment.CommentCreateDate.ToString()上的@comment.CommentCreateDate.ToString()处 } }
这是在post还是get中发生的??你的帖子刚刚回到索引?这是在帖子中发生的。在post控制器的索引视图中,即上面的视图。我想列出该视图中的所有注释。检查您传递给控制器的模型是否有值。我想它是来的NULL@Peru但是我传递给后置控制器的模型,也就是CommentView模型,不是空的,它已经在数据库中输入了数据,然后它在var tempDataComment中可用。。。