如何将下拉列表与一个静态值绑定并从数据库中休息

时间:2015-02-12 07:59:34

标签: asp.net asp.net-mvc razor

我正在使用asp.net mvc我想添加一个dropdown list,它包含一个静态值和来自数据库的其他值

@Html.DropDownListFor(Function(model) model.nDepartmentID, 
  New SelectList(ViewBag.txt, "Value", "Text"), New With 
 {Key .[class] = "select1",Key .style = "width: 150px;"})  

1 个答案:

答案 0 :(得分:0)

使用以下代码

@Html.DropDownListFor(m => m.nDepartmentID, (SelectList)ViewBag.DepartmentList, "Select Any Department", new {@class="select1",@style="width: 150px;" })

你的控制器动作将是

public ActionResult ShowPage()
{
      var deptmnts=db.Departments.ToList();
      ViewBag.DepartmentList=new SelectList(deptmnts,"ID","DepartmentName");
      retun View();
}