绑定在mvc下拉我总是得到这个错误没有ViewData项目类型'IEnumerable'国家有关键我不知道如何对它进行排序没有类型'IEnumerable <SelectListItem>'的ViewData项目,其关键国家
查看
@Html.DropDownList("country",
(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
控制器
List<Companyregister> coun = new List<Companyregister>();
coun = ds.getcountry();
List<SelectListItem> item8 = new List<SelectListItem>();
foreach(var c in coun)
{
item8.Add(new SelectListItem
{
Text = c.country,
Value = c.countryid.ToString()
});
}
ViewBag.countrydrop = item8;
return View();
我不知道我错了,谁能帮助我在此先感谢
我已更新我的代码检查它。 – 2014-10-27 11:07:26
如果你得到这个错误,那么它意味着'ViewBag.coutrydrop'的值是'null'。填充它的代码没有被执行,或者只有当你回发并返回视图时(并且你还没有再次填充'ViewBag.countrydrop') – 2014-10-27 11:08:15
并考虑简化你的代码到'ViewBag.countrydrop = new SelectList(coun,“countryid”,“country”);''和'@ Html.DropDownList(“country”,(SelectList)ViewBag.countrydrop,...)' – 2014-10-27 11:23:03