Kendo dataSource.add - “Id未定义”

时间:2016-02-09 18:53:00

标签: javascript kendo-ui kendo-grid

我正在尝试将一些项目添加到Kendo DataSource,然后在网格中显示。但是,每次我尝试保存时,我都会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行>功能

1 个答案:

答案 0 :(得分:0)

因为你没有设置" Id"对数据源中的任何内容。我有一段时间遇到过类似的问题,Telerik说你必须给出同样的问题'一个有效的唯一值,因为它们在内部使用它来保持数据源的直接性。在您的情况下,您将架构ID设置为" Id",但它看起来不像您的数据源有一个" Id"领域。我最后只是在那里倾倒了一个guid,它解决了我的问题。