abstract:本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法。分享给大家供大家参考,具体如下:一、控制器部分:public ActionResult PageDetail() { var thisList = _sysDepartmentBll.GetAllDepartmentList();//
本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法。分享给大家供大家参考,具体如下:
一、控制器部分:
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 MVC4中使用Html.DropDownListFor的方法示例请关注PHP中文网(www.php.cn)其他文章!