我用调试器检查了MultiSelectList
值的内容已经改变。所以我看到在选择了哪些值之后 selected Value = true;
。
但在我看来,我没有看到。运行时,所选值不可见。最后我尝试了ViewBag
, ViewData
,ListBox()
还有几件事。我没看到什么?
这是我的控制器:
public ActionResult Edit(int id)
{
UserDto user = _userService.GetUserById(id);
var companiesSelectList = new MultiSelectList(_companyService.GetCompanies(), "Id", "Name", user.Companies.Select(x => x.Id.ToString()).ToArray());
TempData["CompaniesList"] = companiesSelectList;
return View(user);
}
我的观点:
<div class="col-md-10">
@Html.ListBoxFor(model => model.Companies, (MultiSelectList)TempData["CompaniesList"], new { @class = "form-control", @required = "required", @multiple = "multiple" })
</div>
我用这个来进行可视化
$(document).ready(function () {
$('#Companies').select2({
placeholder: 'Please Select Company',
maximumSelectionSize: 15,
width: 400,
});
});