设置SelectList选项标签的值

时间:2019-02-08 09:58:08

标签: c# asp.net-mvc

我的MVC应用程序中有一个下拉列表,该列表是从“ PropertyList”模型填充的。

我已如下设置propertyList变量,以获取每个选项所需的详细信息,如下所示;

var propertyList = new SelectList(Model.propertyList, "fullPropertyDetail", "FullAddress");

@Html.DropDownList("addresslist", (SelectList)propertyList, "-- Please select an address from the list below --", new { @id = "valid-addresslist" })

这将按预期填充列表,并且默认选项显示为“-请从列表中选择地址-”,但是该选项的值设置为“”。

有什么方法可以将默认选项值设置为“ none”,因为另一个系统在选择该值时会寻找该值。

2 个答案:

答案 0 :(得分:1)

只需执行以下操作:

@{
   List<SelectListItem> selectListItems = propertyList.ToList();
   selectListItems.Insert(0, (new SelectListItem { Text = "Please select", Value = "none", Selected = true }));
}
@Html.DropDownList("addresslist", selectListItems, new { @id = "valid-addresslist" })

答案 1 :(得分:0)

"-- Please select ..... below --" SelectListItem中将propertyList选项添加为List

赞:new SelectListItem { Text = "-- Please select ..... below --", Value = "none"}

相关问题