没有类型&#39; IEnumerable <selectlistitem>&#39;的ViewData项目。在接受表格后有钥匙

时间:2016-06-01 10:27:10

标签: c# asp.net-mvc html.dropdownlistfor

我已经解决了有关此问题的所有其他问题。尝试过多次发布解决方案,没有效果。

我有两个班级:

public class Player 
{
    public int Id { get; set; }
    [MinLength(1)]
    public string Name { get; set; }
    [MinLength(4)]
    public string LastName { get; set; }
    [DataType(DataType.Date)]
    public DateTime Birthdate { get; set; }

    public virtual Club Club { get; set; }     
}

public class Club 
{
    public int Id { get; set; }
    [StringLength(30, MinimumLength = 3)]
    [Required]
    public string Name { get; set; }
    [DataType(DataType.Date)]
    public DateTime Founded { get; set; }
    public virtual ICollection<Player> Players { get; set; }

    public override string ToString() {
        return Name;
    }
}

Player的创建操作方法中我有:

 // GET: /Players/Create
    public ActionResult Create() {
        //SelectList selectList = new SelectList(db.Clubs, "Id", "Name");
        ViewBag.Clubs = new SelectList(db.Clubs, "Id", "Name");
        return View();
    }

    // POST: /Players/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Name,LastName,Birthdate,Club")] Player player) {
        if (ModelState.IsValid) {
            db.Players.Add(player);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(player);
    }

在我尝试的形式中,我有下拉列表,用于选择玩家所在的俱乐部:

<div class="form-group">
        @Html.LabelFor(model => model.Club, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
      @Html.DropDownListFor(player => player.Club, (SelectList)ViewBag.Clubs)
            @Html.ValidationMessageFor(model => model.Club)
        </div>
    </div>

下拉列表正确呈现,但是当我接受表单时,我会看到黄色的死亡屏幕:There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Club'.

下拉列表看起来像this

2 个答案:

答案 0 :(得分:0)

ViewData仅适用于当前响应。当您从Create的POST重载返回时,没有任何内容可以填充ViewData

这些情况下的正常模式是从POST操作到重定向到GET操作。这样可以避免重复代码并确保刷新页面的用户不会再次发布以前的值。

答案 1 :(得分:0)

您应该在发布后重新生成SelectList。 ViewBag的生命周期仅按请求进行,这就是为什么当你遇到POST失败时它已经被销毁了。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,LastName,Birthdate,Club")] Player player) {
    if (ModelState.IsValid) {
        db.Players.Add(player);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.Clubs = new SelectList(db.Clubs, "Id", "Name");
    return View(player);
}

或者您可以将其重定向到您的GET版本,以便调用您的GET操作,这意味着您无需在发布后重新生成列表。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,LastName,Birthdate,Club")] Player player) {
    if (ModelState.IsValid) {
        db.Players.Add(player);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return RedirectToAction("Create", player);
}