Hi, I'm trying to bind a Kendo treeview to a model of Users & Groups. A group contains a list of users and CAN contain a list of groups (and each group in turn can contain more users and more groups). I'd also like to be able to assign a node an icon depending on whether it's a user node or a group node.
And here's a method to get the data I want to bind to the treeview:
I get a casting error ... Unable to cast object of type 'System.Collections.Generic.List`1[MyApp.Models.Admin.Group]' to type 'System.Collections.Generic.IEnumerable`1[Kendo.Mvc.UI.TreeViewItemModel]'.
Any help would be appreciated, thanks.
The models:
public
class
Group
{
public
int
ID {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
int
? ParentID {
get
;
set
; }
public
List<Group> Groups {
get
;
set
; }
public
List<User> Users {
get
;
set
; }
}
public
class
User
{
public
int
ID {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
int
GroupID {
get
;
set
; }
}
And here's a method to get the data I want to bind to the treeview:
private
IEnumerable<Group> GetUserGroups()
{
List<Group> model =
new
List<Group>
{
new
Group()
{
ID = 1,
Name =
"Admin"
,
ParentID = 1,
Users =
new
List<User>()
{
new
User()
{
ID = 1,
Name =
"John Doe"
,
GroupID = 1
},
new
User()
{
ID = 2,
Name =
"Jane Smith"
,
GroupID = 1
},
},
Groups =
new
List<Group>
{
new
Group()
{
ID = 2,
Name =
"Support"
,
ParentID = 1,
Users =
new
List<User>()
{
new
User()
{
ID = 1,
Name =
"Johnny Support"
,
GroupID = 2
}
}
},
new
Group()
{
ID = 3,
Name =
"Production"
,
ParentID = 1,
Users =
new
List<User>()
{
new
User()
{
ID = 1,
Name =
"Johnny Production"
,
GroupID = 3
}
}
}
}
}
};
return
model;
}
When I try to do the below:
@(
Html.Kendo().TreeView()
.Name(
"treeview"
)
.BindTo((IEnumerable<TreeViewItemModel>)ViewBag.inlineDefault)
)
I get a casting error ... Unable to cast object of type 'System.Collections.Generic.List`1[MyApp.Models.Admin.Group]' to type 'System.Collections.Generic.IEnumerable`1[Kendo.Mvc.UI.TreeViewItemModel]'.
Any help would be appreciated, thanks.