Asp.net 如何使用一个静态值和数据库中的rest绑定DrowDown列表
我正在使用asp.net mvc,我想添加一个Asp.net 如何使用一个静态值和数据库中的rest绑定DrowDown列表,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,我正在使用asp.net mvc,我想添加一个下拉列表,其中包含一个静态值和来自数据库的其他值 @Html.DropDownListFor(Function(model) model.nDepartmentID, New SelectList(ViewBag.txt, "Value", "Text"), New With {Key .[class] = "select1",Key .style = "width: 150px;"}) 使用下面的代码 @Html.DropDownL
下拉列表
,其中包含一个静态值和来自数据库的其他值
@Html.DropDownListFor(Function(model) model.nDepartmentID,
New SelectList(ViewBag.txt, "Value", "Text"), New With
{Key .[class] = "select1",Key .style = "width: 150px;"})
使用下面的代码
@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();
}
静态值是多少?例如“选择国家”作为占位符?请访问了解如何提出一个好问题。我已经更新了标题和你的问题。