我正在开发一个简单的 asp.net mvc 应用程序,但是在多选列表中预选项目时遇到问题。这是我在控制器中的代码:
ViewBag.Authors = new MultiSelectList(db.Authors, "AuthorId", "FullName", db.Authors.Where(a => a.Books.Any(b => b.BookId == id)).Select(a => a.AuthorId));
这是视图内的剃须刀代码:
<div class="editor-label">
@Html.LabelFor(model => model.AuthorIds, "Authors")
</div>
<div class="editor-field">
@Html.ListBoxFor(model => model.AuthorIds, (MultiSelectList)ViewBag.Authors)
@Html.ValidationMessageFor(model => model.AuthorIds)
</div>
它呈现多选列表,但不选择我在代码中设置的项目。有什么建议么?