Telerik Treeview MVC
本文关键字:MVC Treeview Telerik | 更新日期: 2023-09-27 17:48:59
我正在尝试使用c#在MVC中使用Telerik Treeview。我有3个模型,只需要2个层次(根和子)的节点。我需要根节点是第一模型,子节点是第三模型。两者都由第二模型连接。
下面是我写的代码:@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.ToString();
i.Value = item.Id.ToString();
})
)
).Render();
}
在这段代码中,我只能访问Second Model
我也有同样的问题。你需要一些东西从控制器返回json结果。在你的action方法中创建一个TreeViewItem,绑定它并作为jsonresult返回。注意treeview项是如何返回的。您可以根据具体情况返回treeviewitem。
,
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
}
};
}
也许你可以试试这个:
Children(descriptivePattern => descriptivePattern.Items.First().ThirdModelCollection)