My application is asp.net MVC, trying to bind a Telerik MVC Combobox to a model.
Here is the model:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public bool DisplayBold { get; set; }
public string Value
{
get
{
return string.Format("{0}|{1}", this.Id, this.DisplayBold.ToString());
}
}
}
In the controller:
var people = new List<Person>();
people.Add(new Person { Id = 1, Name = "John Doe", DisplayBold = true });
people.Add(new Person { Id = 2, Name = "Jayne Doe", DisplayBold = false });
ViewData["people"] = people;
return View();
I do get the values.
In the view:
<%= Html.Telerik().ComboBox()
.Name("ComboBox")
.BindTo((IEnumerable<SelectListItem>)ViewData["people"])
%>
I get the follwing error:
Unable to cast object of type 'System.Collections.Generic.List`1[caseprog.Models.Person]' to type 'System.Collections.Generic.IEnumerable`1[System.Web.Mvc.SelectListItem]'.
I would appreciate your suggestions. Thanks in advance.
You can’t just cast a List of People to a
IEnumerableofSelectListItem. They’re two different things.Instead, you need to convert the list to a list of
SelectListItem. You can do that in a number of ways, but this one should work: