2012-06-18 44 views
0

我知道这是一个相当简单的问题,但我是C#,ASP.NET MVC3和ADO.Net实体框架的新手,我的朋友谷歌并没有多大帮助我...在GET操作中设置实体键

所以,我有两个实体: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: /Blog/Comment/5 

    [HttpPost] 
    public ActionResult Comment(Comment comment) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Comments.AddObject(comment); 

      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(); 
    } 

我参加了调试模式一坐,和它出现在键设置为空当表单被提交。 如果我在HttpPost方法中设置了postreference,它可以正常工作,但我现在对这个id没有任何线索。 所以,我认为它必须设置在GET部分...

或者,也许我完全走错了方向?

感谢您的任何帮助。

+0

GET参数中的关键是什么?该页面是否以/ Blog/Comment/n提交? – glenatron

+0

是的,的确,只是发现它......完全愚蠢的问题...... –

回答

0

正如之前所说的,完全愚蠢的问题...

// 
    // POST: /Blog/Comment/5 

    [HttpPost] 
    public ActionResult Comment(int id, Comment comment) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Comments.AddObject(comment); 
      comment.PostReference.EntityKey = new EntityKey("BlogEntities.Posts", "id", id); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(); 
    } 

把ID的方法的阿根廷,然后设置参考ADDOBJECT的伎俩后...

希望能对大家帮助像我这样一些迷失的鸟...