Kendo Multiselect:未初始化绑定模型中的选定值
[英] Kendo Multiselect: Selected values from binded model are not initialized
本文介绍了Kendo Multiselect:未初始化绑定模型中的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更新:
缩短问题:
如何使用 Razor 将 SelectList 绑定到 Kendo UI MultiSelect 小部件?
原始问题:
在 ASP.NET MVC 4 应用程序中,我试图让 Kendo Multiselect 工作.我将 Multiselect 小部件绑定到我的模型/视图模型,但未使用初始化值.选择等工作完美.
型号:
公共类数据{公共 IEnumerableSelectedStudents{ 得到;放;}}公开课学生{公共 int Id { 获取;放;}公共字符串名称 { 获取;放;}}
控制器:
列表<学生>学生 = 新列表<学生>();student.Add(new Baumaterial { Id = 1, Name = "Francis" });student.Add(new Baumaterial { Id = 2, Name = "Jorge" });student.Add(new Baumaterial { Id = 3, Name = "Drew" });student.Add(new Baumaterial { Id = 4, Name = "Juan" });ViewBag.Students= new SelectList(students, "Id", "Name");数据数据 = 新数据 { SelectedStudents = new List{2, 4} };返回部分视图(数据);
视图:标准 HTML 完美运行!!
@Html.LabelFor(model => model.SelectedStudents)
<div class="form-field large">@Html.ListBoxFor(model => model.SelectedStudents, (SelectList)ViewBag.Students)
<div class="form-message">@Html.ValidationMessageFor(model => model.SelectedStudents)
查看:剑道多选不工作 --> 多选为空(无预选),但我可以完美选择值
@Html.LabelFor(model => model.SelectedStudents)
<div class="form-field large">@(Html.Kendo().MultiSelectFor(model => model.SelectedStudents).BindTo((SelectList)ViewBag.Students))
<div class="form-message">@Html.ValidationMessageFor(model => model.SelectedStudents)
我做错了什么?感谢您的建议!
解决方案
使用 MultiSelect() 而不是 MultiSelectFor() 并将预选作为字符串列表而不是整数列表传递.
@(Html.Kendo().MultiSelect().Name("SelectedStudents").BindTo(new SelectList(ViewBag.Students, "Id", "Name")).Value(Model.SelectedStudents))
Update:
To shorten the question:
How to bind a SelectList to a Kendo UI MultiSelect Widget using Razor?
Original question:
In an ASP.NET MVC 4 Application, I am trying to get the Kendo Multiselect working. I am binding the Multiselect widget to my model/viewmodel but the init values are not being used. Selecting and so works perfectly.
Model:
public class Data
{
public IEnumerable<int> SelectedStudents{ get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
Controller:
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);
View: Standard-HTML works perfectly!!
<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>
View: Kendo Multiselect not working --> Multiselect is empty (no preselections), but i can select values perfectly
<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>
What I am doing wrong? Thanks for any advice!
解决方案
Using MultiSelect() instead of MultiSelectFor() and pass the preselection as a list of strings instead of a list of integers.
@(Html.Kendo().MultiSelect()
.Name("SelectedStudents")
.BindTo(new SelectList(ViewBag.Students, "Id", "Name"))
.Value(Model.SelectedStudents)
)
这篇关于Kendo Multiselect:未初始化绑定模型中的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!