• Register
1 vote

Problem :

While binding the dropdown in MVC, I often face following error: 

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


Please find below my View for your reference :

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


Please find below my Controller for your reference :

List<Companyregister> mycoun = new List<Companyregister>();
mycoun = ds.getcountry();
List<SelectListItem> myitem8 = new List<SelectListItem>();
foreach( var myc in mycoun )
   myitem8.Add(new SelectListItem
        Text = myc.country,
        Value = myc.countryid.ToString()
ViewBag.mycountrydrop = myitem8;
return View();

I do not know how to resolve above error.

6 5 3
7,540 points

1 Answer

0 votes

Solution :

I had the same issue and tried all the solutions which I knew, and neither of them worked actually after long research I found the solution for the error as below:

In the action please change ViewBag.mycountrydrop = myitem8 to ViewBag.country = myitem8; and in the View please write like below :

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

Actually when you try to write as below

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


Html.DropDownList("country","Select Country)

it looks always in for IEnumerable<SelectListItem> in ViewBag with key country, you can also try to use this overload in this case:

@Html.DropDownList("country","Select country") // it will always look for ViewBag.country and will populate the dropdown

9 7 4
38,600 points

Related questions

1 vote
1 answer 65 views
Problem : Recently I was told to prepare the C# as I am in pool of new projects. So I have started learning C#. But because of some unknown reason to me now I am facing below error. &ldquo;Make sure that the controller has a parameterless public constructor&rdquo;. As I am very new to C# so I am unable to understand the above error. I need help from someone who can fix the above error.
asked Mar 31 morrisBson 3.2k points
0 votes
1 answer 66 views
Problem : I have recently installed a website on my local machine using IIS 7 successfully. But when I tried to deploy it on the live server, I got a following error: "The page cannot be displayed because an internal server error has occurred" Nothing else. Using a same IIS 7 on live and also set to have the Detailed errors in Error Pages module, but still getting a same. What can be the reason?
asked Jan 28 jwilliam 3.9k points
0 votes
1 answer 5 views
0 votes
1 answer 8 views
Problem: I am naive to Asp.Net.I am having a weird looking problem where I am trying to open up my ASP.NET MVC application but I am receiving the below ASP.NET error page which says: Server Error in '/' Application. The resource cannot be found. Description: ... unknown reason to me it seems like it is actually searching for the .aspx file. Please guide me if you think I am missing something.
asked Aug 19 Raphael Pacheco 4.9k points
0 votes
1 answer 25 views
Problem: Currently I have the table called Student in the database with some of the values and I am trying to display the values in the webgrid with the help of entityframework. I have done exactly the same kind of work before many times using the Ado.net, but ... I assign the VAR value to List also is this my correct approach? Kindly guide me in fixing my problem related to Entity Framework.
asked Jul 3 Raphael Pacheco 4.9k points