The syntax is...
@Html.DropDownList("codes")
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello everyone and thanks for the help in advance. I am trying to populate a select list from viewdata and a receiving the error "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'codes'".
My controller code:
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Sick", Value = "%992%" });
items.Add(new SelectListItem { Text = "Well", Value = "%993%" });
ViewBag.codes = new SelectList(items.ToList(), "Value", "Text");
and the view
@Html.DropDownList("codes", ViewBag.codes as SelectList)
I've done quite a few searches, but can't figure out what I am doing wrong. Any help would be appreciated.
The syntax is...
@Html.DropDownList("codes")
This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.
4 deleted commentsComments have been turned off. Learn more
you are storing the list in ViewBag, but trying to access from ViewData. try:
ViewData["codes"] = new List<SelectListItem>
{
new SelectListItem { Text = "Sick", Value = "%992%" },
new SelectListItem { Text = "Well", Value = "%993%" }
};
Your code should work. Using the code copied from your question, dropdown is shown as expected:
I guess that the ViewBag is not properly given to the View. When the ViewBag is not set in the Controller like below:
Your error is reproduced: