2009-10-30 36 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

相关问题