我知道这是一个非常简单的问题,但我是 C#、ASP.NET MVC3 和 ADO.Net Entity Framework 的新手,我的朋友 google 对我帮助不大......
所以,我有两个实体:Post 和 Comments,它们之间有 1-n 的关系。
我想以简单的形式在帖子上发表评论,并带有一条给出帖子ID的路线。
GET 部分(id 是帖子的 id)
//
// GET: /Blog/Comment/5
public ActionResult Comment(int id)
{
Comment comment = new Comment();
comment.PostReference.EntityKey = new EntityKey("BlogEntities.Posts", "id", id);
return View(comment);
}
POST部分:
//
// POST: /Blog/Comment/5
[HttpPost]
public ActionResult Comment(Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.AddObject(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
我在调试模式下骑行,提交表单时似乎密钥设置为空。如果我在 HttpPost 方法中设置后引用,它就可以正常工作,但此时我对 id 没有任何线索。所以,我认为它必须在 GET 部分设置......
或者也许我完全走错了方向?
谢谢你的帮助。