开发者

Database not updating model in MVC

So i just started using ASP.NET MVC and i'm really liking it, except i seem to have an odd knack to encounter the most bizarre of errors. I'm making a simple blogging application for myself. I have two simple models: post and comment. I have a partial view for creating a comment that is embedded in the details view for each post. When i submit the form to update the comment, it goes to my CommentsController's create action, which looks like...

    [HttpPost]
    public ActionResult Create(comment comment)
    {
        comment.date = DateTime.Now;
        if (ModelState.IsValid)
        {
            post p = db.posts.Find(comment.post); /开发者_高级运维/I've verified that comment.post is coming in
            if (p.comments == null) p.comments = new List<comment>();
            p.comments.Add(comment);
            db.Entry(p).State = EntityState.Modified; //I'm using this line since that's how its done in the edit actionmethod of the BlogController. I was just updating db.posts.Find(... manually, but that wasn't workign either.
            db.comments.Add(comment);
            db.SaveChanges();
            return RedirectToAction("Details", "Blog", new  { id = comment.post });
        }

        return PartialView(comment);
    }

The problem is that while the comment gets added to the database just fine, the post doesn't update. When i examine p just before the changes are saved, it's updated, but apparently it never actually commits to database since when i redirect to the Details, those comments aren't there. Is there anything obviously wrong with my code? Am i missing some basic fundamental of .NET or MVC? Let me know if i need to provide more code or context.

Interesting Note: No matter what, post.comments always seems to be null. I set it to an empty list when the post is created, but it still seems to come back null. Not sure if this is just a result of trying to store an empty list or if it has to do with my problem, though. Again, lemme know and i'll stick anything else needed up here.

Thanks!


Perhaps saving the changes is working fine but you don't see the saved comments to a post because you don't load them when you display the post. You can eager load the comments of a post in your action which displays a post like so:

post p = db.posts
    .Include(p1 => p1.comments)
    .Where(p1 => p1.Id == id)
    .SingleOrDefault();

I also think that you can simplify your Create action:

[HttpPost]
public ActionResult Create(comment comment)
{
    comment.date = DateTime.Now;
    if (ModelState.IsValid)
    {
        db.comments.Add(comment);
        db.SaveChanges();

        return RedirectToAction("Details", "Blog", new  { id = comment.post });
    }

    return PartialView(comment);
}

This should work if comment.post is the foreign key of a comment to the related post. (Your code looks like this is the case, because of Find(comment.post))


While @Slauma led me to my solution, I'm just posting my final code i used for future reference (thanks @George Stocker)

    public ActionResult Create(comment comment)
    {
        comment.date = DateTime.Now;
        if (ModelState.IsValid)
        {
            db.comments.Add(comment);
            db.SaveChanges();
            return RedirectToAction("Details", "Blog", new  { id = comment.post });
        }

        return PartialView(comment);
    }

and to retrieve comments...

    public ActionResult Details(int id)
    {
        var post = (from p in db.posts
                     where p.id == id
                     select new { p.id, p.title, p.content, p.date, p.tag, comments = (from c in db.comments where c.post == id select c) }).SingleOrDefault();

        post p2 = new post(post.id, post.title, post.content, post.date,post.tag, post.comments.ToList());
        return View(p2);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜