Reading from here: ASP.NET MVC
Action SelectCategory
has been created inside controller -
public ActionResult SelectCategory() {
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Action", Value = "0"});
items.Add(new SelectListItem { Text = "Drama", Value = "1" });
items.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true });
ViewBag.MovieType = items;
return View();
}
I am not able to understand binding of Data in following line.
@Html.DropDownList("MovieType")
While binding data in similar way,
@Html.DropDownList("IdList");
I obtain following error-
There is no ViewData item of type 'IEnumerable' that has the key 'IdList'.
Controller Action:
public ActionResult SelectId()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "MyId1", Value = "MyId1", Selected=true });
items.Add(new SelectListItem { Text = "MyId2", Value = "MyId2" });
ViewBag.IdList = items;
return View();
}
What am I missing ? Thank you for your help !
See Question&Answers more detail:os