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


I always get this error in the compulsory dropdown in mvc. I do not know how to sort it

View

  @ html.DropDownList ("Country", ViewBag.countrydrop, "Select Country", "Select Country")  

Controller

  list & lt; Companyregister & gt; Country = new list & lt; Company Registration & gt; (); Country = ds.getcountry (); & Lt; SelectListItem & gt; Item8 = new list & lt; SelectListItem & gt; (); Foreach (different C in the country) {item8.Add (New SelectListItem {text = c country, value = c ctrlount.ToString ()}); } ViewBag.countrydrop = item8; See Return ();  

I do not know where im is wrong, anyone can thank me in advance

Change your action to ViewBag.countrydrop = item8 to ViewBag.country = item8; and type as follows:

  @ Html.DropDownList ("Country", ViewBag.country, "Select country" (IEnumerable & lt; SelectListItem & gt;)) < / Code> 

Actually when you write

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

or

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

This key country with IEnumerable & lt; SelectListItem & gt; In you can also use this surcharge in this case:

  @ Html.DropDownList ("Country", "Country Select ) "// This will look for ViewBag.country and displays the dropdown  

View


Comments