【发布时间】:2016-02-21 16:08:46
【问题描述】:
我的 .cshtml 中有这个
@Html.DropDownListFor(m => m.Attendance.Time02InId, new SelectList(Model.ClockingLocations, "Id", "Name"), new {@class = "form-control select2-allow-clear"})
Model.ClockingLocations = Clock 列表
public class Clock
{
public int Id { get; set; }
public string Name { get; set; }
}
这是我准备好的文档的 jquery
var placeholder = "Select...";
$(".select2-allow-clear").prepend("<option></option>");
$(".select2-allow-clear").select2({
allowClear: true,
placeholder: placeholder,
width: null
});
但是,当m.Attendance.Time02InId 没有值时,我的选择列表仍然显示我的Model.ClockingLocations 的第一项,而不是我的占位符。
【问题讨论】:
-
只需使用内置方法添加标签选项 -
@Html.DropDownListFor(m => m.Attendance.Time02InId, new SelectList(Model.ClockingLocations, "Id", "Name"), "Select...", new {@class = "form-control select2-allow-clear"})并删除placeholder: placeholder, -
@StephenM,这适用于剃刀语法。
标签: javascript jquery asp.net-mvc select2