“没有 'IEnumerable<SelectListItem>' 类型的 ViewData 项具有键 'Skip'

"There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Skip'

我正在尝试创建一个球员团队,每个团队有 4 名球员,所以我有 4 个 select 列表,其中包含并填充联盟中的球员。当我去救队时报错:

"There is no ViewData item of type 'IEnumerable' that has the key 'Skip'`

CreateTeam 控制器:

public ActionResult CreateLeagueTeam(int? id)
{
    int leagueId = id.Value;
    League league = db.Leagues.Include(x=>x.Members).First(l=> l.Id == leagueId);
    var leagueMemberList = league.Members.ToList();
    var leagueMemberSelectListItems = new List<SelectListItem>();
    foreach(Member m in leagueMemberList)
    {
        leagueMemberSelectListItems.Add(new SelectListItem() { Text = m.FirstName + " " + m.LastName, Value = m.Id.ToString() });
    }
    ViewData["leagueMemberList"] = leagueMemberSelectListItems;
    return View();  
}

HTTP POST ActionResult:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateLeagueTeam([Bind(Include = "skip")] Team team)
{           
    team.IsBonspiel = false;
    team.IsLeague = true;
    // team.Name = team.Skip.LastName;
    team.YearActive = games.getSeasonYear();
    //finds and sets the league ID for the team
    int leagueId = (int)Session["leagueId"];
    League league = db.Leagues.Find(leagueId);
    team.League = league;
    if (ModelState.IsValid)
    {
        db.Teams.Add(team);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(team);
}

最后是抛出错误的视图:

<div class="form-group">
    @Html.Label("Skip", null, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Skip, (IEnumerable<SelectListItem>)ViewBag.leagueMemberList, htmlAttributes: new { @class = "control-label col-md-2" })
    </div>
</div>

还有团队模型:

public class Team: Entity
{
    [Required]
    public bool IsBonspiel { get; set; }
    [Required]
    public bool IsLeague { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int YearActive { get; set; }
    public int? SkipId { get; set; }
    public Member Skip { get; set; }
    public string BonspielSkip { get; set; }
    public List<BonspielTeamMember> BonspielMembers { get; set; }
    public List<Member> Members { get; set; }
    public List<Game> Games { get; set; }
    public Bonspiel Bonspiel { get; set; }
    public League League { get; set; }
}

您向 "ViewData" 添加了数据,但您尝试从 ViewBag 访问。 您应该知道 ViewData 与 ViewBag 不同。 因此,您尝试更改此行:

 ViewData["leagueMemberList"] = leagueMemberSelectListItems;

至 => ViewBag.leagueMemberList = leagueMemberSelectListItems;

您的代码存在多个错误。

  1. Skip 是一个复杂对象,您不能将 <select> 绑定到 复杂的对象。根据你的看法,应该是 @Html.DropDownListFor(m => m.SkipId, ...)
  2. 您没有 ViewBag 属性 名为 leagueMemberList (将控制器代码更改为 ViewBag.leagueMemberList = leagueMemberSelectListItems; 或将视图代码更改为 @Html.DropDownListFor(m => m.SkipId, (IEnumerable<SelectListItem>)ViewData["leagueMemberList"], ...)
  3. 您没有重新分配 ViewData["leagueMemberList"] 的值(或 ViewBag.leagueMemberList) 在 POST 方法中,如果 ModelState 是 无效并且您 return 视图,这将导致不同的结果 异常,因为它将是 null(参考 this question/answer
  4. 您在 POST 方法中的 [Bind(Include = "skip")] 代码意味着 ModelState 将始终无效,因为您排除了所有 属性 来自绑定的模型(除了 Skip 而你没有 post Skip)
  5. 的任何值