更新
缩短问题:
如何使用Razor将SelectList绑定到Kendo UI MultiSelect小部件?
原始问题:
在ASP.NET MVC 4应用程序中,我试图让Kendo Multiselect工作。我将Multiselect小部件绑定到我的模型/ viewmodel但是没有使用init值。选择等完美无缺。
型号:
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>
查看:Kendo Multiselect无效 - &gt;多选是空的(没有预选),但我可以完美地选择值
<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>
我做错了什么?谢谢你的建议!
答案 0 :(得分:8)
使用MultiSelect()代替MultiSelectFor()并将预选作为字符串列表而不是整数列表传递。
@(Html.Kendo().MultiSelect()
.Name("SelectedStudents")
.BindTo(new SelectList(ViewBag.Students, "Id", "Name"))
.Value(Model.SelectedStudents)
)