有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'具有主要国家 [英] There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country
问题描述
在MVC约束力的下拉我总是得到这个错误。那些我不知道如何对它进行排序键的类型IEnumerable的国家无ViewData的项目
我的看法
@ Html.DropDownList(国家,(IEnumerable的&LT; SelectListItem&GT;)ViewBag.countrydrop,选择国家)
我的控制器
列表&LT; Companyregister&GT; COUN =新的List&LT; Companyregister&GT;();
{
COUN = ds.getcountry();
};
清单&LT; SelectListItem&GT; item8 =新的List&LT; SelectListItem&GT;();
的foreach(在COUN变种C)
{
item8.Add(新SelectListItem
{
文= c.country,
值= c.countryid.ToString()
});
} ViewBag.countrydrop = item8;
#endregion
返回查看();
我不知道我错了,谁能帮助我在此先感谢
在你的行动改变 ViewBag.countrydrop = item8
到 ViewBag.country = item8;
并查看这样写:
@ Html.DropDownList(国家,
(IEnumerable的&LT; SelectListItem&GT;)ViewBag.country,
选择国家)
其实,当你写
@ Html.DropDownList(国家,
(IEnumerable的)ViewBag.country,
选择国家)
块引用>
块引用>或
Html.DropDownList(国家,选择国家)
块引用>
块引用>它看起来在
的IEnumerable&LT; SelectListItem&GT;
在ViewBag
与关键的国家你也可以在这种情况下使用这个过载:@ Html.DropDownList(国家,选择国家)//它会寻找ViewBag.country并填充下拉列表
请参阅工作演示示例
in binding dropdown in mvc i always get this error There is no ViewData item of type 'IEnumerable' country that has the key i dont know how to sort it
my view
@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
my controller
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; #endregion return View();
i dont know where im wrong ,can anyone help me thanks in advance
解决方案In your action change
ViewBag.countrydrop = item8
toViewBag.country = item8;
and in View write like this:@Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag.country, "Select country")
Actually when you write
@Html.DropDownList("country", (IEnumerable)ViewBag.country, "Select country")
or
Html.DropDownList("country","Select Country)
it looks in for
IEnumerable<SelectListItem>
inViewBag
with key country, you can also use this overload in this case:@Html.DropDownList("country","Select country") // it will look for ViewBag.country and populates dropdown
这篇关于有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'具有主要国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!