2011-06-06 63 views
0

所以我刚开始使用ASP.NET MVC,我很喜欢它,除了我似乎有一个奇怪的诀窍,遇到最奇怪的错误。我正在为自己制作一个简单的博客应用程序。我有两个简单的模型:postcomment。我有一个局部视图,用于创建每个帖子的详细信息视图中嵌入的评论。当我提交表单更新注释,它进入我CommentsController的创建行动,它看起来像......数据库没有在MVC中更新模型

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

的问题是,虽然评论被添加到数据库中就好了,后没有按” t更新。当我在保存更改之前检查p时,它已更新,但显然它从未实际提交到数据库,因为当我重定向到Details时,那些注释不存在。我的代码有什么明显的错误吗?我是否缺少.NET或MVC的一些基本基础?让我知道如果我需要提供更多的代码或上下文。

有趣的提示:无论如何,post.comments似乎总是空白。我在创建帖子时将其设置为空列表,但它似乎仍然返回null。不知道这是否仅仅是试图存储一个空列表的结果,或者是否与我的问题有关。再次,我知道,我会坚持任何其他需要在这里。

谢谢!

回答

3

也许保存更改工作正常,但您没有看到保存的帖子的评论,因为您在显示帖子时未加载它们。您可以渴望加载在你的行动后,显示后,像这样的评论:

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

我也认为你可以简化您创建行动:

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

这应该工作,如果comment.post是对相关帖子发表评论的外键。 (你的代码看起来是这样的话,因为Find(comment.post)

+0

没有,我包括在细节上每篇文章的评论查看与'@foreach(在Model.comments VAR项目)'...加,我知道他们没有得到保存,因为下次我尝试添加评论时,“p.comments”仍然为空。谢谢。 – 2011-06-06 16:22:00

+0

我确实喜欢在ActionMethod中抓取匹配的注释,而不是从post.comments加载。如果我无法解决这个问题,我会使用它。谢谢! – 2011-06-06 16:24:50

+0

@Thomas Shields:你不使用延迟加载,对吧? (否则'p.comments'不能为'null',它至少是一个空集合。)如果你不使用延迟加载,上面代码中的'p.comments'将总是*为'null' ,无论你在数据库中有多少评论。你如何加载帖子的详细信息视图的评论?您的foreach循环中的“Model.comments”是否为空? – Slauma 2011-06-06 16:31:23

0

虽然@Slauma使我对我的解决方案,我只是发表我的我用于未来参考(感谢@George施托克尔)

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); 
    } 
最终代码

和检索评论...

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