There is no ViewData item of type 'IEnumerable' that has the key country

后端 未结 11 2228
鱼传尺愫
鱼传尺愫 2020-12-03 07:52

While binding dropdown in MVC, I always get this error: There is no ViewData item of type \'IEnumerable\' that has the key country.

<
11条回答
  •  旧时难觅i
    2020-12-03 08:12

    In your action change ViewBag.countrydrop = item8 to ViewBag.country = item8;and in View write like this:

    @Html.DropDownList("country",
                       (IEnumerable)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 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

提交回复
热议问题