2009-10-30 56 views
1

我有以下代碼:沒有ViewData的項目類型的鍵「值」「的IEnumerable <SelectListItem>」

// GET: /PlayRoundHole/Create 

public ActionResult Create(int id) 
{ 
    DB db = new DB(); 

    var t = db.Players.ToList(); 
    IList<Player> player = db.Players.ToList(); 
    IEnumerable<SelectListItem> selectList = from c in player 
     select new SelectListItem 
     { 
      Text = c.FirstName + " " + c.LastName, 
      Value = c.PlayerID.ToString() 
     }; 


    this.ViewData["Players"] = new SelectList(selectList, "Value", "Text", ""); 

    this.ViewData["RoundID"] = id; 

    return View();    
} 

// 
// POST: /PlayRoundHole/Create 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Create(FormCollection collection) 
{ 
    try 
    { 
     DB db = new DB(); 

     PlayRound playRound = new PlayRound(); 
     playRound.PlayerID = Int64.Parse(Request.Form["Players"]); 
     playRound.TenantID = 1; 
     playRound.RoundID = Int64.Parse(Request.Form["RoundID"].ToString()); 
     playRound.Score = 0; 

     var playRoundHoles = from prh in db.PlayRoundHoles.ToList() 
      from hl in db.Holes.ToList() 
      where prh.HoleID == hl.HoleID 
      where prh.PlayRoundID == Int64.Parse(Request.Form["RoundID"].ToString()) 
      select new { prh.HoleID, hl.Sequence }; 

     foreach(var a in playRoundHoles) 
     { 
      PlayRoundHole playRoundHole = new PlayRoundHole(); 
      playRoundHole.HoleID = a.HoleID; 
      playRoundHole.Stroke = Byte.Parse(Request.Form["PlayRoundHoleID_" + a.Sequence].ToString()); 
      playRound.PlayRoundHoles.Add(playRoundHole); 
     } 
     db.SubmitChanges(); 

     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

我得到當交上述錯誤消息返回。

<td><%= Html.DropDownList("Value", (IEnumerable<SelectListItem>)ViewData["Players"])%></td> 

不知道爲什麼造成它?

回答

3

發現錯誤?

如果你的代碼碰到catch,它將返回到你的「Create」視圖,如果在ViewData中既沒有指定「Player」或「Value」,也沒有在你的post操作中設置這兩個視圖, 。

+0

沒有抓到其他事情的錯誤。這樣的流程:創建控制器(OK)>然後顯示創建屏幕視圖>數據輸入(點擊提交)>創建控制器(後)>它嘗試保存然後它再次進入頁面(​​<%= Html.DropDownList( 「Value」,(IEnumerable )ViewData [「Players」)出現錯誤 – dcpartners 2009-10-31 00:26:04

+0

沒關係Joel,我發現了罪魁禍首......我看了一眼,發現了一個問題,謝謝指出。 – dcpartners 2009-10-31 01:03:43

相關問題