0

我有以下代码绑定到 @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。如何 ?

4

2 回答 2

2

您可以使用以下重载

@Html.DropDownListFor(
    model => model.CompanyID, 
    Model.Companies, 
    "-- select a company --", 
    new { style = "width:300px;" }
)
于 2012-08-24T12:30:11.707 回答
0

@Html.DropDownListFor(model => model.CompanyID, Model.Companies, " ", new { style = "width:300px;" })

于 2012-08-24T12:32:15.833 回答