返回PartialView时,ApplicationUser为null

时间:2016-08-05 16:48:55

标签: c# asp.net-mvc entity-framework

我有一个注释类,在创建时存储用户的id,根据我的理解,实体框架将此Id映射到用户。这在最初加载视图时有效。但是当我添加一个新的注释,并由于某种原因返回PartialView时,Author(应用程序用户)为null并抛出异常。但是,如果我重新加载页面,则会显示新注释,并显示正确的ApplicationUser信息。

 public class Comment
{
    public Guid ID { get; set; }
    public string Text { get; set; }


    [Required]
    public string AuthorId { get; set; }

    [ForeignKey("AuthorId")]
    public virtual ApplicationUser Author { get; set; }

    public DateTime DateSubmitted { get; set; }
    public Guid PostID { get; set; }
    public virtual Post Post { get; set; }

}

我使用ViewModel处理视图上的数据。

 public class CommentViewModel
{
    public string Text { get; set; }
    public Guid PostID { get; set; }
    public List<Comment> CommentList { get; set; }
}

这是我的控制器动作。

 public ActionResult CreateComment(CommentViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var currentUser = manager.FindById(User.Identity.GetUserId());

            var comment = new Comment
            {
                ID = Guid.NewGuid(),
                AuthorId = currentUser.Id,
                DateSubmitted = DateTime.Now,
                PostID = viewModel.PostID,
                Text = viewModel.Text

            };

            Debug.Write("PostID:" + comment.PostID + "Author: " + "Text: " + comment.Text);
            db.Comments.Add(comment);
            db.SaveChanges();
            CommentViewModel model = new CommentViewModel();
            model.CommentList = db.Comments.Where(i => i.PostID == comment.PostID).OrderByDescending(c => c.DateSubmitted).ToList();
            return PartialView(model);

        }
        else
        {
            Debug.Write("not valid");

        }

        return PartialView(viewModel);

    }

此foreach显示CommentList中的所有注释。提交新评论时,它会在显示@ item.Author.Id的行上崩溃。

foreach (var item in Model.CommentList)
    {


        <div class="row">
            <div class="col-sm-1">
                <div class="thumbnail">
                    <img class="img-responsive user-photo" src="https://ssl.gstatic.com/accounts/ui/avatar_2x.png">
                </div><!-- /thumbnail -->
            </div><!-- /col-sm-1 -->

            <div class="col-sm-5">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <strong>@item.Author.Id</strong>

                        <time class="timeago" datetime="@item.DateSubmitted"></time>

                    </div>
                    <div class="panel-body">
                        @item.Text
                    </div><!-- /panel-body -->
                </div><!-- /panel panel-default -->
            </div><!-- /col-sm-5 -->
        </div>

    }

1 个答案:

答案 0 :(得分:1)

Comment.Author是虚拟的,因此您需要在保存后明确加载它。变化:

model.CommentList = db.Comments.Where(i => i.PostID == comment.PostID).OrderByDescending(c => c.DateSubmitted).ToList();

要:

model.CommentList = db.Comments
    .Include(c => c.Author)
    .Where(i => i.PostID == comment.PostID)
    .OrderByDescending(c => c.DateSubmitted).ToList();

请参阅https://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx#Anchor_1