Kendo dataSource.add - "Id is not defined"

Kendo dataSource.add - "Id is not defined"

本文关键字:quot is not defined Id dataSource add Kendo      更新时间:2023-09-26

我正在尝试将一些项目添加到剑道数据源中,然后在网格中显示。但是,每次我尝试保存时,我都会Reference Error: Id is not defined.

起初我以为这是因为我没有在我的架构中包含Id,但我检查了一下,它看起来没问题。

var viewModel = new kendo.observable({
orgDataSource: new kendo.data.DataSource({
    transport: {
        read: {
            url: "/Organization/GetAll",
            dataType: "json"
        },
        update: {
            url: "/Host/Organization/Edit",
            dataType: "json",
            type: "POST",
            data: {
                __RequestVerificationToken: getAntiForgeryToken()
            }
        },
        create: {
            url: "/Host/Organization/Create",
            dataType: "json",
            type: "POST",
            data: {
                __RequestVerificationToken: getAntiForgeryToken()
            }
        },
        destroy: {
            url: "/Host/Organization/Delete",
            dataType: "json",
            type: "POST",
            data: {
                __RequestVerificationToken: getAntiForgeryToken()
            }
        }
    },
    schema: {
        model: {
            id: "Id",
            fields: {
                Id: { type: "number", editable: false, nullable: true },
                Name: { type: "string" },
                LicenseExpiration: { type: "date" },
                LicenseNumber: { type: "number" },
                Active: { type: "boolean" },
                CreateDate: { type: "date" },
                LastModDate: { type: "date" },
                AvailableLicenses: { type: "string" },
                State: { type: "string" }
            }
        },
        errors: "errorMsg"
    },
    pageSize: 20,
    error: function (e) {
        toastr.options = {
            "positionClass": "toast-bottom-full-width"
        };
        toastr.error("There was an error: " + e.errors, "Uh, Oh!");
        this.cancelChanges();
    },
    serverPaging: false,
    serverFiltering: false,
    serverSorting: false
}),
reloadOrganizations: function () {
    this.get("orgDataSource").read();
},
onOrgSave: function (e)
{
    var uid = $('input[name="OrgRowUID"]').val();
    var tr = $('tr[data-uid="' + uid + '"]'); // get the current table row (tr)
    var name = $('input[name="Name"]').val();
    var licenseNumber = $('input[name="LicenseNumber"]').val();
    var licenseExpiration = $('input[name="LicenseExpiration"]').val();
    var email = $('input[name="Email"]').val();
    var state = $('input[name="State"]').val();
    var logo = $('input[name="ImageUrl]').val();
    var active = $('input[name="Active"]').is(":checked");
    // get the data bound to the current table row
    var orgGrid = $("#OrganizationGrid").data("kendoGrid");
    var data = orgGrid.dataItem(tr);
    if (data == null)
    {
        viewModel.orgDataSource.add({ Name: name, LicenseNumber: licenseNumber, LicenseExpiration: licenseExpiration, Email: email, State: state, ImageUrl: logo, Active: active })
        orgGrid.saveChanges();
        viewModel.orgDataSource.sync();
        viewModel.reloadOrganizations();
    } else {
        data.set("Name", name);
        data.set("LicenseNumber", licenseNumber);
        data.set("LicenseExpiration", licenseExpiration);
        data.set("Email", email);
        data.set("State", state);
        data.set("ImageUrl", logo);
        data.set("Active", active);
    }
    $("#orgCreateModal").modal('hide');
    $("#orgEditModal").modal('hide');
}
});

此行上发生错误:

viewModel.orgDataSource.add({ Name: name, LicenseNumber: licenseNumber, LicenseExpiration: licenseExpiration, Email: email, State: state, ImageUrl: logo, Active: active });

FireBug 中的错误是:

引用错误:未定义 ID - kendo.all.min.js第 25 行>函数

它抛出该错误是因为您没有将"Id"设置为数据源中的任何内容。 不久前我遇到了类似的问题,Telerik 说你必须给"id"一个有效的唯一值,因为他们在内部使用它来保持数据源中的直线。 在本例中,您将架构 ID 设置为"Id",但看起来您的数据源没有"Id"字段。 我最终只是在那里倾倒了一个 guid,它解决了我的问题。