I am trying to work with Telerik Treeview in MVC with C#. I have 3 models and need just 2 levels (root and child) of nodes. I need that the root node is the First Model, and the child node the Third Model. Both are linked by the Second Model.
Below is the code that I have done:
@using Hant.Material.ValueObject.Domain
@using Hant.Material.Web.Models
@model IEnumerable<DescriptivePatternModel>
@{
Html.Telerik().TreeView()
.Name("treeView")
.ExpandAll(true)
.BindTo(Model, mapping => mapping
.For<DescriptivePatternModel>(binding => binding
.Children(descriptivePattern => descriptivePattern.Items)
.ItemDataBound((i, descriptivePattern) =>
{
i.Text = descriptivePattern.Name;
i.Value = descriptivePattern.Id.ToString();
})
)
.For<ItemModel>(binding => binding
.ItemDataBound((i, item) =>
{
i.Text = item.VersionDate.ToStri开发者_开发百科ng();
i.Value = item.Id.ToString();
})
)
).Render();
}
In this code I can only access the Second Model.
I had the same issue. You need something to return a json result from your controller. Create a TreeViewItem in your action method, bind it and return it as a jsonresult. Notice how treeview items are returned. You may just return the treeviewitem depending on your case.
eg:
public JsonResult RefreshGroups(Group currentGroup, int rootUserGroupId)
{
parentIds = new List<int> { rootUserGroupId };
var groupsTree = new List<Group> { GetGroupHierarchy(currentGroup, rootUserGroupId) };
var treeViewItem = new TreeViewItem();
treeViewItem.BindTo(groupsTree, mappings =>
{
mappings.For<Group>(binding => binding
.ItemDataBound((item, group) =>
{
item.Text = group.GroupName;
item.Value = group.GroupID.ToString();
item.LoadOnDemand = true;
})
.Children(group => group.SubGroups));
});
return new JsonResult
{
Data = new
{
ExpandedNodes = parentIds,
Nodes = treeViewItem.Items
}
};
}
Maybe you can try this:
Children(descriptivePattern => descriptivePattern.Items.First().ThirdModelCollection)
精彩评论