ASP.NET MVC4中使用Html.DropDownListFor的方法示例

 更新时间:2021年9月22日 10:06  点击:1633

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《asp.net优化技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

[!--infotagslink--]

相关文章