2012-03-26 54 views
0

你好的人,我有以下代码:外键不填充MVC3

public ActionResult Create(GameTBL gametbl) 
     { 
      if (ModelState.IsValid) 
      { 
       //First you get the gamer, from GamerTBLs 
       var gamer = db.GamerTBLs.Where(k => k.UserName == User.Identity.Name).SingleOrDefault(); 
       //Then you add the game to the games collection from gamers 
       gamer.GameTBLs.Add(gametbl); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
     } 

这是给我下面的错误:

Error 1 'MvcApplication1.Controllers.GameController.Create(MvcApplication1.Models.GameTBL)': not all code paths return a value 

这段代码是试图做的是试图填充玩家的外键进入游戏桌

模型我的控制器玩家:

public string UserName { get; set; } 
    public int GamerID { get; set; } 
    public string Fname { get; set; } 
    public string Lname { get; set; } 
    public string DOB { get; set; } 
    public string BIO { get; set; } 
我的游戏控制器

型号:

public int GameID { get; set; } 
    public string GameName { get; set; } 
    public string ReleaseYear { get; set; } 
    public string Cost { get; set; } 
    public string Discription { get; set; } 
    public string DownloadableContent { get; set; } 
    public string Image { get; set; } 
    public string ConsoleName { get; set; } 
    public int GamerIDFK { get; set; } 
    public byte[] UserName { get; set; } 

回答

0

试试这个... return语句应该是if语句外......问题是,当ModelState中是不是你没有返回一个视图/动作结果有效...

public ActionResult Create(GameTBL gametbl) 
    { 
     if (ModelState.IsValid) 
     { 
      //First you get the gamer, from GamerTBLs 
      var gamer = db.GamerTBLs.Where(k => k.UserName == User.Identity.Name).SingleOrDefault(); 
      //Then you add the game to the games collection from gamers 
      gamer.GameTBLs.Add(gametbl); 
      db.SaveChanges(); 
      return RedirectToAction("Index");    
     } 
     return View(gametbl); 
    } 
+0

我会试着让你知道,谢谢 – user1137472 2012-03-26 23:44:58

+0

如果我理解正确,你仍然试图填充你的控制器模型的“GamerIDFK”属性,仍然没有从玩家表中填充外键到游戏表 – user1137472 2012-03-26 23:50:51

+0

@ user1137472?你可以发表你的看法代码,看看你与此GamerIDFK属性在视图中做什么...... – NiK 2012-03-27 14:59:19

3

您只需要在您的ModelState无效时返回视图。

public ActionResult Create(GameTBL gametbl) 
    { 
     if (ModelState.IsValid) 
     { 
      //First you get the gamer, from GamerTBLs 
      var gamer = db.GamerTBLs.Where(k => k.UserName == User.Identity.Name).SingleOrDefault(); 
      //Then you add the game to the games collection from gamers 
      gamer.GameTBLs.Add(gametbl); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(gametbl); 
    } 

这将使页面显示模型创建中的任何错误(假设您有验证)。

+0

还没填充从玩家表游戏桌 – user1137472 2012-03-26 23:51:08

+0

外键抛出任何错误?或者它只是没有保存?你可以输入一个断点并确保玩家被设置,然后确保gametbl被添加到玩家?你已经完成了怎样的深度调试? – 2012-03-27 15:02:25

0

只要你知道,错误是不是真的ASP.Net MVC相关 - 这将是一个返回值的任何方法的错误。

错误消息not all code paths return a value的意思就是说 - 当方法签名说明它应该的时候,通过代码的路径没有返回值。

对于您的情况,您的操作方法具有签名ActionResult Create(GameTBL gametbl),因此通过该方法的所有路径必须返回ActionResult。在代码中,确实返回一个ActionResult发生时ModelState.IsValid是真实的道路 - 但没有什么是在ModelState.IsValid是假的路径返回。

其他的答案给你如何通过的“ModelState.IsValid是假的”路线返回一个ActionResult纠正你的代码示例。

+0

这就是你说,但我仍然有问题填充玩家的外键进入游戏,当我运行我的应用程序外键不被填充 – user1137472 2012-03-26 23:49:15

+0

我的答案(和其他用户)的所有细介绍了如何解决您的错误信息。至于为什么你的外键没有保存 - 这是一个不同的问题,并将与您的数据访问代码相关 – StanK 2012-03-27 00:31:07