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.