Kendo Multiselect:未初始化从绑定模型中选择的值 [英] Kendo Multiselect: Selected values from binded model are not initialized

查看:43
本文介绍了Kendo Multiselect:未初始化从绑定模型中选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

要简化这个问题:

如何使用Razor将SelectList绑定到Kendo UI MultiSelect窗口小部件?

How to bind a SelectList to a Kendo UI MultiSelect Widget using Razor?

原始问题:

在ASP.NET MVC 4应用程序中,我试图使Kendo Multiselect正常工作.我将Multiselect小部件绑定到我的模型/视图模型,但是未使用init值.选择,因此效果很好.

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.

型号:

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);

视图:Standard-HTML完美运行!!

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>

视图:Kendo Multiselect不起作用-> Multiselect为空(无预选),但我可以完美地选择值

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!

推荐答案

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆