9

更新:

为了缩短问题:

如何使用 Razor 将 SelectList 绑定到 Kendo UI MultiSelect Widget?

原始问题:

在 ASP.NET MVC 4 应用程序中,我试图让 Kendo Multiselect 工作。我将 Multiselect 小部件绑定到我的模型/视图模型,但未使用初始化值。选择等完美地工作。

模型:

public class Data
{
    public IEnumerable<int> SelectedStudents{ get; set; }
}

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
}

控制器:

List<Student> students = new List<Student>();
students.Add(new Baumaterial { Id = 1, Name = "Francis" });
students.Add(new Baumaterial { Id = 2, Name = "Jorge" });
students.Add(new Baumaterial { Id = 3, Name = "Drew" });
students.Add(new Baumaterial { Id = 4, Name = "Juan" });

ViewBag.Students= new SelectList(students, "Id", "Name");
Data data = new Data { SelectedStudents = new List<int>{2, 4} };

return PartialView(data);

观点:标准 HTML 完美运行!!

<div class="form-label">
    @Html.LabelFor(model => model.SelectedStudents)
</div>
<div class="form-field large">
    @Html.ListBoxFor(model => model.SelectedStudents, (SelectList)ViewBag.Students)
</div>
<div class="form-message">
    @Html.ValidationMessageFor(model => model.SelectedStudents)
</div>

视图:剑道多选不起作用-->多选是空的(没有预选),但我可以完美地选择值

<div class="form-label">
    @Html.LabelFor(model => model.SelectedStudents)
</div>
<div class="form-field large">
    @(Html.Kendo().MultiSelectFor(model => model.SelectedStudents)
        .BindTo((SelectList)ViewBag.Students)
    )
</div>
<div class="form-message">
    @Html.ValidationMessageFor(model => model.SelectedStudents)
</div>

我做错了什么?感谢您的任何建议!

4

1 回答 1

10

使用 MultiSelect() 而不是 MultiSelectFor() 并将预选作为字符串列表而不是整数列表传递。

@(Html.Kendo().MultiSelect()
    .Name("SelectedStudents")
    .BindTo(new SelectList(ViewBag.Students, "Id", "Name"))
    .Value(Model.SelectedStudents)
)
于 2013-05-27T15:38:21.517 回答