• Register
1 vote
98 views

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")

or

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

0 votes
1 answer 6 views
6 views
Problem: Is there any way to get the viewdata item of type 'ienumerable<selectlistitem>' that has the key ?
asked Apr 27 salim1 60.6k points
0 votes
1 answer 3 views
3 views
Problem: Someone help me to find the viewdata item of type 'ienumerable<selectlistitem> .
asked Apr 27 salim1 60.6k points
0 votes
1 answer 35 views
35 views
Problem: to allow get requests set jsonrequestbehavior to allowget.
asked Feb 23 Ethan ross 2.7k points
0 votes
1 answer 6 views
6 views
Problem: I have some code and when it executes, it throws an exception saying: The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo What does this mean, and how do I fix it?
asked Mar 20 ummesalma 29.2k points
1 vote
1 answer 365 views
365 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 5 views
5 views
Problem: How to fix this error > Could not load file or assembly 'system.web.helpers' or one of its dependencies
asked Apr 28 ummeshani 10.8k points
0 votes
1 answer 17 views
17 views
Problem: I have a web application that uses Forms Authentication. The application is MVC and also has a section for requests from our Mobile application. The mobile application for a client was unused for a while and came back and now I see this logged in ... at this post: ASP.NET Forms Authentication failed for the request. Reason: The ticket supplied has expired Any help is appreciated. Thanks,
asked Apr 25 Humaira ahmed 50.7k points
0 votes
1 answer 163 views
163 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 11 views
11 views
Problem: Does anyone know the solution of it? So kindly let me know .. Asp net mvc 4 sample project download
asked Apr 27 Ifra 37.2k points
0 votes
1 answer 44 views
44 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