How can i create a SelectList with multiple selected items?. I want to bind the SelectList to Razor dropdown and if its a single selected item i am using this code
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);
}
And in Razor i used the syntax
<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>
But how can i set mutiple items as selected on this SelectList?I tried
ViewBag.RolesList = new SelectList(roles, "Id", "RoleName",, new[] { 2, 3, 10, 11 });
But multiple items are not seems to be set as selected. How can i do this?