我有以下代码绑定到@ Html.DropDownListFor:
视图模型
public static UserManagementViewModel CreateCompanyMappingViewModel(List<Company> company)
{
UserManagementViewModel viewModel = new UserManagementViewModel();
viewModel.Companies = new SelectList(company, "CompanyID", "CompanyName");
return viewModel;
}
}
CSHTML
@Html.DropDownListFor(model => model.CompanyID, Model.Companies, new { style = "width:300px;" })
下拉列表已成功绑定。我需要的是,在DropDownList中插入一个空白行并在绑定到DropDownList时首先选择它。怎么样?
答案 0 :(得分:2)
您可以使用following overload:
@Html.DropDownListFor(
model => model.CompanyID,
Model.Companies,
"-- select a company --",
new { style = "width:300px;" }
)
答案 1 :(得分:0)
@ Html.DropDownListFor(model =&gt; model.CompanyID,Model.Companies,“”,new {style =“width:300px;”})