有型无ViewData的项目'的IEnumerable< SelectListItem>'具有主要国家 [英] There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country

查看:195
本文介绍了有型无ViewData的项目'的IEnumerable< SelectListItem>'具有主要国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC约束力的下拉我总是得到这个错误。那些我不知道如何对它进行排序键的类型IEnumerable的国家无ViewData的项目

我的看法

  @ Html.DropDownList(国家,(IEnumerable的< SelectListItem>)ViewBag.countrydrop,选择国家)

我的控制器

 列表< Companyregister> COUN =新的List< Companyregister>();
        {
            COUN = ds.getcountry();
        };
        清单< SelectListItem> item8 =新的List< SelectListItem>();
        的foreach(在COUN变种C)
        {
            item8.Add(新SelectListItem
                {
                    文= c.country,
                    值= c.countryid.ToString()
                });
        }        ViewBag.countrydrop = item8;
        #endregion
        返回查看();

我不知道我错了,谁能帮助我在此先感谢


解决方案

在你的行动改变 ViewBag.countrydrop = item8 ViewBag.country = item8; 并查看这样写:

  @ Html.DropDownList(国家,
                   (IEnumerable的< SelectListItem>)ViewBag.country,
                   选择国家)

其实,当你写


  

    

@ Html.DropDownList(国家,
                           (IEnumerable的)ViewBag.country,
                           选择国家)


  


  

    

Html.DropDownList(国家,选择国家)


  

它看起来在的IEnumerable< SelectListItem> 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 to ViewBag.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> in ViewBag 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

See Working DEMO Example

这篇关于有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'具有主要国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆