Kendo Multiselect: Selected values ​​from a bound model are not initialized

Update:

To shorten the question:

How to link SelectList with MultiSelect widgets for Kendo UI using Razor?

The original question:

In an ASP.NET MVC 4 application, I am trying to get Kendo Multiselect to work. I bind the Multiselect widget to my model / viewmodel, but the init values ​​are not used. Choice works fine anyway.

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

 <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 choice), but I can choose the values ​​fine

 <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 am I doing wrong? Thanks for any advice!

+6
source share
1 answer

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) ) 
+5
source

Source: https://habr.com/ru/post/944978/


All Articles