DropdownList的处理总结

时间:2022-02-08 10:10:06
创建一:
List<SelectListItem> items = new List<SelectListItem>()
{
new SelectListItem(){Text="Text1",Value="Value1",Selected=false},
new SelectListItem(){Text="Text2",Value="Value2",Selected=true},
new SelectListItem(){Text="Text3",Value="Value3",Selected=false},
new SelectListItem(){Text="Text4",Value="Value4",Selected=false}
};
ViewData["Categories"] = items;
前台:
@Html.DropDownListFor(model => model.ClassId, ViewData["Categories"] as List<SelectListItem>"请选择") 创建二:
List<Models.T_CMS_Class> list = db.T_CMS_Class.ToList();
SelectList items = new SelectList(list, "ClassId", "ClassName");
ViewBag.ClassId = items;
前台:
@Html.DropDownListFor(model => model.ClassId, ViewBag.ClassId as SelectList, "请选择")
@Html.ListBoxFor(model => model.ClassId, ViewBag.ClassId as List<SelectListItem>"请选择") 创建三:
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["Categories"] = items;
前台:
@Html.DropDownListFor(model => model.ClassId, ViewData["Categories"] as List<SelectListItem>"请选择") 创建四:
BLL.Sorts sortbll = new BLL.Sorts();
ViewBag.Select = sortbll.GetList().Select(d => new SelectListItem()
{
Text = d.Sname,
Value = d.Id.ToString(),
Selected = (model.Sid == d.Id) ? true : false
}); 前台:
@Html.DropDownListFor(model => model.Sid, ViewBag.Select as List<SelectListItem>, "请选择") 创建5
ViewBag.MusicType = new SelectList(typebll.GetList(), "TypeId", "TypeName");
ViewBag.MusicType = new SelectList(typebll.GetList(), "TypeId", "TypeName", MusicType);