2012-03-26 53 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