0
如何创建具有多个选定项目的SelectList ?.我想选择列表的剃刀下拉绑定,如果它的一个选择的项目我使用这个代码具有多个选定项目的SelectList
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);
}
而且在剃刀我使用的语法
<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>
但我怎么可以设置多发项目,如我试过
ViewBag.RolesList = new SelectList(roles, "Id", "RoleName",, new[] { 2, 3, 10, 11 });
但是多个项目似乎没有被设置为选定。我怎样才能做到这一点?
我的代码更改为 ViewBag.ActionsList = new MultiSelectList(actions,“Id”,“Name”,new [] {2,3,10,11}); 但它返回异常 无法将类型为“System.Web.Mvc.MultiSelectList”的对象转换为键入“System.Web.Mvc.SelectList”。 所以有什么其他的方式与Dropdownlist一起使用 –
行为是IEnumerable吗?字段'Id'和'Name'是否存在? – AlexB
解决了这个错误。但仍然选择不工作 代码 ViewBag.ActionsList = new MultiSelectList(actions,“Id”,“Name”,new [] {2,3,10,11}); and in razor Html.DropDownListFor(model => model.ActionId,(MultiSelectList)ViewBag.ActionsList,new {class =“multiselect”,multiple =“”,style =“width:450px; height:300px”}) 但没有选择任何项目。我的情况有什么不对? –