【问题标题】:Telerik Treeview MVCTelerik 树视图 MVC
【发布时间】:2011-04-27 00:10:18
【问题描述】:

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

在这段代码中,我只能访问第二个模型。

【问题讨论】:

    标签: c# model-view-controller telerik


    【解决方案1】:

    我有同样的问题。你需要一些东西来从你的控制器返回一个 json 结果。在您的操作方法中创建一个 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
                                      }
                       };
        }
    

    【讨论】:

      【解决方案2】:

      也许你可以试试这个:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多