• 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

Please log in or register to answer this question.

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 189 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, 2020 morrisBson 3.2k points
0 votes
1 answer 4 views
Problem: to allow get requests set jsonrequestbehavior to allowget.
asked 2 days ago Ethan ross 1.8k points
0 votes
1 answer 126 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, 2020 jwilliam 3.9k points
0 votes
1 answer 10 views
0 votes
1 answer 23 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 30 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 649 views
Problem : Facing following ASP.Net error an asynchronous module or handler completed while an asynchronous operation was still pending.
asked Nov 14, 2019 peterlaw 6.9k points
0 votes
1 answer 7 views
Problem: are you missing a using directive or an assembly reference.
asked Feb 18 charles mathews 3.8k points
0 votes
0 answers 8 views
Problem: cara mengatasi datatables warning table id table ajax error
asked Feb 17 Dan phillip 1.9k points
0 votes
1 answer 55 views
Problem : I am using the Membership.create user function, then a below error is occurring: &ldquo;The required anti-forgery form field "__RequestVerificationToken" is not present&rdquo; How can I resolve above error?
asked Jan 21, 2020 jwilliam 3.9k points