第一种方式:
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "001", Value = "1", Selected = false });
items.Add(new SelectListItem() {Text = "002", Value = "2", Selected = false });
ViewData["items"] = items;
简化后:
var items = new List<SelectListItem>()
{
(new SelectListItem() {Text = "001", Value = "1", Selected = false}),
(new SelectListItem() {Text = "002", Value = "2", Selected = false})
};
将items值给ViewData:
ViewData["items"] = items;
cshtml中使用:
@Html.DropDownListFor(x=>x.Department, (IEnumerable<SelectListItem>)ViewData["items"], "请选择")