綁定在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