Vinculando DropDownList ao MVC View
Lendo daqui:ASP.NET MVC
AçaoSelectCategory
foi criado dentro do controlador -
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();
}
Não consigo entender a vinculação de dados na linha a seguir.
@Html.DropDownList("MovieType")
Enquanto vincula dados de maneira semelhante,
@Html.DropDownList("IdList");
Eu obtenho seguinte error-
Não há nenhum item ViewData do tipo 'IEnumerable' que tenha a chave 'IdList'.
Ação do Controlador:
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();
}
O que estou perdendo ? Obrigado pela ajuda !