我在视图中为我的下拉列表分配值,如下所示。
@Html.DropDownList("Sites", new SelectList(Model.SiteList.Select(x => new { Value = x.SiteId, Text = x.SiteName }), "Value", "Text"))
如何添加另一个名为'--Select--'
答案 0 :(得分:2)
您只需将其添加为最后一个参数:
@Html.DropDownList("Sites",
new SelectList(Model.SiteList.Select(x =>
new { Value = x.SiteId, Text = x.SiteName }), "Value", "Text"), "--Select--");
<强> Working Fiddle 强>
答案 1 :(得分:1)
@{
var values = Model.SiteList.Select(x => new SelectListItem() { Value = x.SiteId, Text = x.SiteName }).ToList();
values.Add(new SelectListItem() { Text = "--Select--" });
}
@Html.DropDownList("Sites", new SelectList(values, "Value", "Text"))