我有这个ListBoxFor:
<div class="col-sm-12">
<div class="form-group">
@Html.LabelFor(m => m.RolesList.Roles)
@Html.ListBoxFor(m => m.RolesList.RoleIds, new MultiSelectList(Model.RolesList.Roles, "Value", "Text", Model.RolesList.RoleIds), new { @class = "form-control col-sm-12 js-select2" })
@Html.ValidationMessageFor(m => m.RolesList.RoleIds)
</div>
</div>
加载页面时,我可以选择多个项目并将其保存到数据库中。但是,在编辑页面时,什么也没有选择。
这是列表框的视图模型
public class ApplicationMultiRolesDropdownListViewModel
{
[Display(Name = "Roles")]
public List<string> RoleIds { get; set; }
public IEnumerable<SelectListItem> Roles { get; set; }
}
我不确定要显示所选项目还需要做什么。
这就是我的控制器的外观
DocumentFieldDetailsViewModel dfdvm = new DocumentFieldDetailsViewModel();
var rolesData = new List<SelectListItem>();
applicationRolesData.GetAll().ForEach(
x => rolesData.Add(new SelectListItem
{
Value = x.RoleId,
Text = x.RoleName + " (" + x.ApplicationName + ")"
})
);
ApplicationMultiRolesDropdownListViewModel ardlvm = new ApplicationMultiRolesDropdownListViewModel();
ardlvm.Roles = new SelectList(rolesData, "Value", "Text");
var result = documentFieldsData.Get(Id);
var permissions = documentFieldPermissionsData.GetAll(result.Id);
ardlvm.RoleIds = permissions.Select(x => x.Id).ToArray();
dfdvm = new DocumentFieldDetailsViewModel
{
DocumentCategoryId = DocumentCategoriesId,
Id = Id,
FieldName = result.Name,
Order = result.Order,
RolesList = ardlvm
};