从远程数据加载父节点和子节点

时间:2016-06-06 21:44:00

标签: asp.net-mvc kendo-ui kendo-treeview

我的控制器中有一个方法返回List<TreeViewItemModel>(),我填充了正确的层次结构。这似乎是正确序列化,但是当我加载Treeview时,我没有任何层次结构,只是第一级节点。

示例:

Treeview

上面的每个课程都有2/3个场景,我已经验证过,从课程表开始,它们被作为项添加到基础对象=&gt; TreeViewItemModel

控制器:

public JsonResult GetAvailableCurricula(string LocationId)
{
    LocationId = "1";
    if(LocationId != string.Empty)
    {
        var results = Logic.GetFilteredCurriculum().Select(c => CurriculumToTreeView(c));
        return Json(results, JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(new List<TreeViewItemModel>(),JsonRequestBehavior.AllowGet);
    }
}

private TreeViewItemModel CurriculumToTreeView(CurriculumModel c)
{
    var tree = new TreeViewItemModel()
    {
        Id = c.CurriculumId.ToString(),
        Text = c.CurriculumName,
        HasChildren = c.Scenarios.Any()
    };

    if (tree.HasChildren)
    {
        tree.Items = c.Scenarios.Select(scenario =>
            new TreeViewItemModel()
            {
                Text = scenario.Name,      
            }
        ).ToList();    
    }
    return tree;
}

查看:

@(Html.Kendo().TreeView()
.Name("AvailableCurricula")
.DataTextField("Text")
.DataSource(source => source
    .Read(read => read
        .Action("GetAvailableCurricula", "TraineeAssignments")
    .Data("filterAvailableCurricula")
    )
)

我需要采取一些额外的步骤来绑定子对象和父对象,而不是一次只有一个级别吗?我有一小部分数据,我不需要经常重新加载,所以我希望避免单独/按需加载每个级别。

如果它有帮助,这里是我从控制器发送我的一个课程的原始JSON:

{"Enabled":true,"Expanded":false,"Encoded":true,"Selected":false,"Text":"Operator B","SpriteCssClass":null,"Id":"1","Url":null,"ImageUrl":null,"HasChildren":true,"Checked":false,"Items":[{"Enabled":true,"Expanded":false,"Encoded":true,"Selected":false,"Text":"test 2","SpriteCssClass":null,"Id":null,"Url":null,"ImageUrl":null,"HasChildren":false,"Checked":false,"Items":[],"HtmlAttributes":{},"ImageHtmlAttributes":{},"LinkHtmlAttributes":{}},{"Enabled":true,"Expanded":false,"Encoded":true,"Selected":false,"Text":"Scenario II","SpriteCssClass":null,"Id":null,"Url":null,"ImageUrl":null,"HasChildren":false,"Checked":false,"Items":[],"HtmlAttributes":{},"ImageHtmlAttributes":{},"LinkHtmlAttributes":{}}],"HtmlAttributes":{},"ImageHtmlAttributes":{},"LinkHtmlAttributes":{}}

1 个答案:

答案 0 :(得分:-1)

我相信Treeview的远程数据选项使用ajax来加载子数据,无论是按需还是初始加载 - 由LoadOnDemand option控制。

剑道文档中的remote data examples就是这样。这就是我在a previous project上实现它的方式。完整的代码示例包括树视图和网格。