将远程数据绑定到kendo树视图

时间:2015-09-23 12:32:59

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

我尝试将从控制器操作方法获取的值返回到kendo树视图,以将模块Name伪装为kendo树视图中的父节点。

public ActionResult LoadTreeView([DataSourceRequest] DataSourceRequest request)
    {
        using (var countryvar = new TGSFMSSys_UserEntities())
        {
            IQueryable<Module> module = countryvar.Modules;
            DataSourceResult result = module.ToDataSourceResult(request, value => new Module
            {
                ModuleId = value.ModuleId,
                ModuleName = value.ModuleName

            });
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }

以下是我创建的剑道树视图的Javascript代码

<script>
    $("#treeview").kendoTreeView({
        checkboxes: {
            checkChildren: true
        },
        check: onCheck,
        dataSource:
        {
            transport: {
                read: '/MapModuleTask/LoadTreeView'
            }
        },
        dataTextField: 'ModuleName'
    });
</script>

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

控制器:HomeController.cs

public JsonResult LoadTreeView(int? id)
{
    List<Module> modules = new List<Module>();

    int temp = id.GetValueOrDefault();

    modules.Add(new Module() { ModuleId = 1 + temp, ModuleName = "Name1_" + temp , hasChildren = false });
    modules.Add(new Module() { ModuleId = 2 + temp, ModuleName = "Name2_" + temp, hasChildren = true });

    return Json(modules, JsonRequestBehavior.AllowGet);
}

型号:

public class Module
{
    public int ModuleId { get; set; }
    public string ModuleName { get; set; }
    public Boolean hasChildren  { get; set; }
}

查看:

<div id="treeview"></div>
<script>

    var datasource = new kendo.data.HierarchicalDataSource({
        transport: {
            read: function (options) {
                var id = options.data.ModuleId; 
                $.ajax({
                    url: '/Home/LoadTreeView',
                    dataType: "json",   
                    data: { id: id },
                    success: function (result) {
                         options.success(result);
                    },
                    error: function (result) {
                         options.error(result);
                    }
                });
            }
        },
        schema: {
            model: {
                id: "ModuleId",
                hasChildren: "hasChildren"
            }
        }
    });

    $("#treeview").kendoTreeView({
        checkboxes: {
            checkChildren: true
        },
        dataSource: datasource,
        dataTextField: 'ModuleName'
    });
</script>

如果有任何疑虑,请告诉我。