在我看来,我有一个DropDownList,它使用数据库数据填充。但是当我运行它时,出现一个我不太明白的错误:使用数据库数据填充DropDownList时出错
“没有ViewData项的类型'IEnumerable'具有'DdlBagTypes'键。
我不知道如何做到这一点,但我抬头各种解决方案,这是我要做的事:
函数从数据库中获取数据:
public IEnumerable<SelectListItem> getBagelTypes()
{
return (from t in Db.BagelType.AsEnumerable()
select new SelectListItem
{
Text = t.Name,
Value = t.BagelTypeId.ToString(),
}).AsEnumerable();
}
控制器:
public ActionResult Index()
{
ViewData["LstBagels"] = DbO.getBagels();
TempData["LstTypeOptions"] = DbO.getBagelTypes();
Session["OrderCount"] = OrderCount;
return View();
}
查看:
@model BestelBagels.Models.Bagel
@{
ViewBag.Title = "Home Page";
var LstBagels = ViewData["LstBagels"] as List<BestelBagels.Models.Bagel>;
var LstTypeOptions = TempData["LstTypeOptions"] as IEnumerable<SelectList>;
var OrderCount = Session["OrderCount"];
}
@Html.DropDownList("DdlBagTypes", (SelectList)LstTypeOptions)
这一个让我所有 时间。检查'LstTypeOptions'是否为'null' – CodingIntrigue