javascript - Kendo UI Grid Javascript 数据源调用 Controller 操作

标签 javascript kendo-ui telerik kendo-grid kendo-datasource

我在将我的 JavaScript kendo ui 网格绑定(bind)到来自操作方法的模型数据时遇到问题。我看到的所有示例主要是 MVC 包装器,而 JavaScript 示例各不相同,似乎没有一个适合我。

这是我在下方的位置。

我用有效的静态数据做了一个通用测试。

var dataSource_Test = new kendo.data.DataSource({
        data: [{ LeagueDetailGroupId: "15", GroupName: "Best Team 5"}]
});

这是我试图通过 Controller 操作创建的数据源对象:

var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "@Url.Action("LeagueDetailGroup_Read", "Configuration")?_leagueTypeId=" + leagueTypeId,
                    // i have tried all kinds of variants here, and not sure what to put
                    // my action method is returning json using kendo's DataSourceResult method
                    //contentType: "application/json",
                    type: "POST"
                    //dataType: "odata"
                },
                schema: {
                    data: "Data", // seen this in examples, dunno what it does
                    total: "Total", // seen this in examples, dunno what it does
                    model: {
                        id: "LeagueDetailGroupId",
                        fields: {
                            LeagueDetailGroupId: { editable: false, nullable: true },
                            GroupName: { validation: { required: true } }
                        }
                    }
                },          
                // i seen this is an example from telerik but dont understand the use case for it                       
                parameterMap: function (data, operation) {
                    // this prints no data before i even start so its a moot point configuring it from products to my stuff at this moment
                    // but not sure what todo here of if i need this anyways
                    console.log(data);          
                    if (operation != "read") {
                        // post the products so the ASP.NET DefaultModelBinder will understand them
                        var result = {};
                        for (var i = 0; i < data.models.length; i++) {
                            var product = data.models[i];

                            for (var member in product) {
                                result["products[" + i + "]." + member] = product[member];
                            }
                        }
                        return result;                                 
                    } else {
                        return JSON.stringify(data)
                    }
                }
            }
        });

这是与通用静态数据源对象一起工作的网格。

var grid = $("#leagueEdit_ldg_grid").kendoGrid({
                        dataSource: dataSource,
                        sortable: true,
                        pageable: true,
                        autobind: false,
                        //detailInit: leagueEdit_ldg_detailInit,
                        dataBound: function () {
                            this.expandRow(this.tbody.find("tr.k-master-row").first());
                        },
                        columns: [
                            {
                                field: "LeagueDetailGroupId",
                                title: "Group Id",
                                width: "110px"
                            },
                            {
                                field: "GroupName",
                                title: "Group Name",
                                width: "110px"
                            }
                        ]
                    });         

延迟读取,自动绑定(bind)设置为 false。

dataSource.read();

这是我简化的 Controller 操作。它运行并获取数据,并且适用于我的 MVC 包装器网格。

    [Route("LeagueDetailGroup_Read/{_leagueTypeId:int}")]
    public ActionResult LeagueDetailGroup_Read([DataSourceRequest]DataSourceRequest request, int _leagueTypeId = -1)
    {
       DataSourceResult result =
           _unitOfWork.FSMDataRepositories.LeagueDetailGroupRepository.Get(
               ld => ld.LeagueTypeId == _leagueTypeId
               )
        .ToDataSourceResult(request,
            ld => new LeagueDetailGroupViewModel
        {

            LeagueDetailGroupId = ld.LeagueDetailGroupId,
            LeagueTypeId = ld.LeagueTypeId,
            GroupName = ld.GroupName,
            DateCreated = ld.DateCreated,
            DateLastChanged = ld.DateLastChanged
        }
        );
        // data looks fine here
        return Json(result, JsonRequestBehavior.AllowGet);
    }

目前我遇到这个错误:

Uncaught TypeError: e.slice is not a function
    at init.success (kendo.all.js:6704)
    at success (kendo.all.js:6637)
    at Object.n.success (kendo.all.js:5616)
    at i (jquery-3.1.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-3.1.1.min.js:2)
    at A (jquery-3.1.1.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-3.1.1.min.js:4)

最佳答案

如果不进行测试很难知道,但让我知道这是如何工作的。

更改您的 Controller ,以便您只返回一个 json 字符串。 此外,尝试删除您的模式和参数映射,并将您的 dataType 设置为 json:

var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "@Url.Action("LeagueDetailGroup_Read", "Configuration")?_leagueTypeId=" + leagueTypeId,
                dataType: "json"
            }
        }
});

对于网格,我发现简单的 json 数据通常不需要定义架构/模型。 Kendo 非常烦人且难以调试。让我知道进展如何。

关于javascript - Kendo UI Grid Javascript 数据源调用 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45718513/

相关文章:

javascript - 使用类组件在 React Router v4 中嵌套路由

javascript - 单击按钮时调用 Jade mixin 函数

javascript - 如何检测已在 Kendo UI 网格中创建网格行?

asp.net - 在 RadPanelItem 中添加 RadTextBox

c# - 从 ASP.NET 用户控件执行方法

jquery - Kendo UI 在.net 中导出为 PDF 多页

javascript - Laravel Ajax 调用 url 到 Controller

javascript - 在 TypeScript 中使用分布在多个模块文件中的命名空间

jquery - 如何从 Kendo Grid 数据源将对象发送到 Controller ?

javascript - 仅在按下 Enter 时允许剑道网格服务器过滤 API 调用