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

asp net there is no viewdata item of type ienumerable selectlistitem that has the key
dropdownlistfor but must be of type 'ienumerable<selectlistitem
system invalidoperationexception there is no viewdata item of type that has the key
dropdownlist in mvc
selectlist in mvc
dropdownlist in mvc 5 razor example
mvc selectlist from database
list<selectlistitem

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

View

@Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")

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;
return View();

I don't know how to resolve it.

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

[SOLVED], In your action change ViewBag.countrydrop = item8 to ViewBag.country = item8; and in View write like this: @Html.DropDownList("country"  There is no ViewData item of type 'IEnumerable ' that has the key "key" asp.net-mvc-5 c# entity-framework-6 razor

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.

There is no ViewData item of type, The mistake is you are putting it in ViewBag.ProcedureSubCategoryID while you are passing ProcedureID int Html.DropDownList() and also  There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'xxx' 141 The ViewData item that has the key 'MY KEY' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

Note that a select list is posted as null, hence your error complains that the viewdata property cannot be found.

Always reinitialize your select list within a POST action.

For further explanation: Persist SelectList in model on Post

There is no ViewData item of type 'IEnumerable , you have not cast the viewstate to IEnumerable<SelectListItem> that's why its giving error try this it will work. Hide Copy Code. There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'xxx'. There are a couple of posts about this on Stack Overflow but none with an answer that seem to fix the problem in my current situation. I have a page with a table in it, each row has a number of text fields and a dropdown.

you can use this:

 var list = new SelectList(countryList, "Id", "Name");
 ViewBag.countries=list;
 @Html.DropDownList("countries",ViewBag.countries as SelectList)

MVC5 DropDownList: There is no ViewData item of type, There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType'. [Answered]RSS. 15 replies. Last post Apr 16,  Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key Sep 27, 2011 11:56 AM | tr0llphysics | LINK Interesting point, but the reason I didn't set it in HandleForm is because the DDL is part of my view.

try this

@Html.DropDownList("ddlcountry",(List<SelectListItem>)ViewBag.countrydrop,"Select country")

In Controller

ViewBag.countrydrop = ds.getcountry().Select(x => new SelectListItem { Text = x.country, Value = x.countryid.ToString() }).ToList();

ASP.Net MVC Error: There is no ViewData item of type , MVC5 DropDownList: There is no ViewData item of type 'IEnumerable<​SelectListItem>' that has the key 'Distributor'. I'm having trouble interacting with a​  There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key " . RSS

'There is no ViewData item of type 'IEnumerable SelectListItem' that , Getting Error There is no ViewData item of type IEnumerableSelectListItem that has the key State public class user_detail { public int User_id  There is no viewdata item of type 'ienumerable<selectlistitem>' that has the key 'dept.departmentid'.

getting error There is no ViewData item of type 'IEnumerab, 'There is no ViewData item of type 'IEnumerable SelectListItem' that has the key Duration: 15:19 Posted: Jan 11, 2020

There is no ViewData item of type 'IEnumerable , There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CostCenterId'."} this my contrller action method.

Comments
  • i have updated my code check it.
  • If you are getting this error, then it means that the value of ViewBag.coutrydrop is null. Either the code for populating it is not being executed, or this is only occurring when you post back and return the view (and you have not populated ViewBag.countrydrop again)
  • And consider simplifying your code to ViewBag.countrydrop = new SelectList(coun, "countryid", "country"); and @Html.DropDownList("country",(SelectList)ViewBag.countrydrop, ...)
  • If your open the demo in the accepted answer, and change ViewBag.country = item8; to ViewBag.countrydrop = item8; and @Html.DropDownList("country","Select country") to @Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country") i.e. as per your posted code you will see that it also works. The issue is not the naming, its the fact that the ViewBag property is null which you can test by now changing ViewBag.countrydrop = item8; to ViewBag.countrydrop = null;
  • Is the error happening in the initial load, or just when you post back and return the view?
  • Or something like this: MySelectList = new List<SelectListItem>();
  • Way too common scenario: error happens on POST, so you return the view model to the page with an error. But you didn't refresh the List<SelectListItem>'s which were lost in the POST - so they're null. Instead of a null-ref, get a really misleading error from the View saying that some property is not of a particular type when you know full well it's not supposed to be of that type etc
  • @mmcrae exactly, I had this problem a couple of times with POST action, where I forgot to populate my dropdown items again.
  • The comment "property was null" was key, indicating the viewbag item did not get populated for some reason.
  • are you able to get countries in your list?
  • @dazzling kumar change your dropdownlist name.I have updated my code check it and change your DropDownList name in view country instead of ddlcountry then its works.