如何创建包含多个选定项目的 SelectList?我想将 SelectList 绑定到 Razor 下拉列表,如果它是一个选定的项目,我正在使用此代码
List<SchoolApp.Models.Accounts.role> roles = accRepo.GetRoles();
if (selected > 0)
{
ViewBag.RolesList = new SelectList(roles, "Id", "RoleName");
}
else
{
ViewBag.RolesList = new SelectList(roles, "Id", "RoleName", selected);
}
在 Razor 中,我使用了语法
<div class="editor-label">
@Html.LabelFor(model => model.RoleId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.RoleId, (SelectList)ViewBag.RolesList, "Select", new { @class = "multiselect", @multiple = "multiple", @style = "width: 450px;height:300px" })
@Html.ValidationMessageFor(model => model.RoleId)
</div>
但是我怎样才能在这个 SelectList 上设置多个项目?我试过了
ViewBag.RolesList = new SelectList(roles, "Id", "RoleName",, new[] { 2, 3, 10, 11 });
但是多个项目似乎没有被设置为选中。我怎样才能做到这一点?