本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法。分享给大家供大家参考,具体如下:
一、控制器部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public ActionResult PageDetail() { var thisList = _sysDepartmentBll.GetAllDepartmentList(); //数据源 //添加一条默认数据 var resultList = new List<SelectListItem> { new SelectListItem {Text = "--请选择--" , Selected = true , Value = "" } }; //将数据源添加到resultList集合中 resultList.AddRange(thisList.Select(thisModel => new SelectListItem { Text = thisModel.DepartmentName, Selected = false , Value = thisModel.DepartmentId })); ViewBag.DepartmentList= GetDepartmentSelectList(_sysDepartmentBll.GetAllDepartmentList()); return View(); } |
二、PageDetail.cshtml部分
复制代码 代码如下:
@Html.DropDownListFor(m => m.DepartmentId, ViewBag.DepartmentList as IEnumerable<SelectListItem>, new { id = "ddlDepartment"})
或者:
复制代码 代码如下:
@Html.DropDownListFor(m => m.DepartmentId, (List<SelectListItem>)ViewBag.DepartmentList, new { id = "ddlDepartment"})
希望本文所述对大家asp.net程序设计有所帮助。