1、下拉框代码方式
控制器内构建下拉项目:
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem() { Text = "计算机", Value = "computer" });
list.Add(new SelectListItem() { Text = "数学", Value = "math" });
ViewData["deptName"] = list;
视图中:
<p>所属部门:@Html.DropDownListFor(m => m.deptName, ViewData["deptName"] as List<SelectListItem>)</p>
2、下拉框数据库方式
控制器内通过数据库构建下拉项目:
ViewData["AreaId"] = from a in rp.GetArea()
select new SelectListItem {
Text=a.AreaName,
Value=a.AreaId.ToString()
};
试图中:
<p>所属区域:@Html.DropDownListFor(m => m.AreaID, ViewData["AreaID"] as List<SelectListItem>)</p>