Đọc từ đây: ASP.NET MVCDropDownList Binding vào MVC Xem
Action SelectCategory
đã được tạo ra bên trong bộ điều khiển -
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();
}
Tôi không thể hiểu được ràng buộc của dữ liệu trong sau dòng.
@Html.DropDownList("MovieType")
Trong khi ràng buộc dữ liệu theo cách tương tự,
@Html.DropDownList("IdList");
tôi được sự thoả sau sai sót
There is no ViewData item of type 'IEnumerable' that has the key 'IdList'.
điều khiển hành động:
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();
}
W mũ tôi thiếu? Cảm ơn sự giúp đỡ của bạn !
Tất cả mọi thứ có vẻ là tốt. Có thể bạn nên thử @ Html.DropDownList (ViewBag.IdList); –
Html.DropDownList có 8 (tám) quá tải cho phép bạn chỉ định mọi thứ bao gồm danh sách bạn muốn hiển thị trong các tùy chọn. Xin vui lòng đi qua các tài liệu của Html.DropDownList đây –
thử này Html.DropDownList ("anyname", ViewBag.IdList) - constructor này chấp nhận hai params - tên và danh sách bạn muốn hiển thị –