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


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


@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 =,
        Value = c.countryid.ToString()

ViewBag.countrydrop = item8;
return View();

I don't know how to resolve it.

This question is tagged with c#

~ Asked on 2014-10-27 10:29:14

The Best Answer is


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

                   "Select country")

Actually when you write

@Html.DropDownList("country", (IEnumerable), "Select country")


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 and populates dropdown

See Working DEMO Example

~ Answered on 2014-10-27 10:47:06


If you were using DropDownListFor like this:

@Html.DropDownListFor(m => m.SelectedItemId, Model.MySelectList)

where MySelectList in the model was a property of type SelectList, this error could be thrown if the property was null.

Avoid this by simple initializing it in constructor, like this:

public MyModel()
    MySelectList = new SelectList(new List<string>()); // empty list of anything...

I know it's not the OP's case, but this might help someone like me which had the same error due to this.

~ Answered on 2017-01-25 02:36:20

Most Viewed Questions: