簡體   English   中英

Telerik Treeview MVC

[英]Telerik Treeview MVC

我正在嘗試使用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();
}

在這段代碼中,我只能訪問第二個模型。

也許你可以試試這個:

Children(descriptivePattern => descriptivePattern.Items.First().ThirdModelCollection)

我遇到過同樣的問題。 你需要一些東西來從你的控制器返回一個json結果。 在action方法中創建一個TreeViewItem,綁定它並將其作為jsonresult返回。 請注意如何返回樹視圖項。 你可以根據你的情況返回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
                                  }
                   };
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM