@Html.DropDownList 是如何绑定ViewData、ViewBag数据的?
作者:互联网
The DropDownList helper used to create an HTML select list requires a IEnumerable , either explicitly or implicitly. That is, you can pass the IEnumerable explicitly to the DropDownList helper or you can add the IEnumerable to the ViewBag using the same name for the SelectListItem as the model property.
可以传入明确的IEnumerable<SelectListItem>,也可以通过ViewBag或者ViewData隐式地传入,前提是需要相同的名称,比如:
ViewBag.GenreId或者ViewData["GenreId"]。官方示例:
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 }); items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" }); ViewBag.MovieType = items; ViewData["HourList"] =items ;
return View(); }
视图:
@Html.DropDownList("MovieType")
以上来自:https://q.cnblogs.com/q/78044/
不相同名称的处理:
@Html.Kendo().DropDownListFor(l => l.lb).OptionLabel("Select...").DataTextField("Text").DataValueField("Value").BindTo(ViewData["HourList"] as List<SelectListItem>))
标签:ViewData,ViewBag,items,Value,Html,Text,new,SelectListItem 来源: https://www.cnblogs.com/djd66/p/15166450.html