简体   繁体   English

返回PartialView时ApplicationUser为null

[英]ApplicationUser is null when returning PartialView

I have a comment class that when created stores the id of the user, and from my understanding the entity framework maps this Id to the user. 我有一个注释类,该注释类在创建时存储用户的ID,据我了解,实体框架将此ID映射到用户。 This works when initially loading the view. 最初加载视图时,此方法有效。 But when I add a new comment, and return the PartialView for some reason the Author (application user) is null and an exception is thrown. 但是,当我添加新注释并出于某种原因返回PartialView时,Author(应用程序用户)为null,并引发异常。 However, if I reload the page the new comment is shown with the correct ApplicationUser information displayed. 但是,如果我重新加载页面,则会显示新注释,并显示正确的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; }

}

I use a ViewModel to handle the data on the view. 我使用ViewModel处理视图上的数据。

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

Here is my controller Action. 这是我的控制器动作。

 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);

    }

This foreach displays all comments from the CommentList. 此foreach显示CommentList中的所有评论。 When submitting a new comment, it crashes on the line that should be displaying @item.Author.Id. 提交新评论时,它在应显示@ 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>

    }

Comment.Author is virtual so you need to explicitly load it after saving. Comment.Author是虚拟的,因此您需要在保存后显式加载它。 Change: 更改:

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

To: 至:

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

See https://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx#Anchor_1 参见https://msdn.microsoft.com/zh-cn/library/gg671236%28v=vs.103%29.aspx#Anchor_1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM