Kendo UI Grid 再次发回已插入的行 [英] Kendo UI Grid posting back already Inserted Rows again

查看:19
本文介绍了Kendo UI Grid 再次发回已插入的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,当插入成功完成并且如果我继续插入另一行时,在下一次插入中它也会发送之前成功插入的行,所以它是这样的.在第一次插入时,该行被回发到 webAPI 并成功插入.在下一个插入时发送两行,其中一个来自第一步.在第三次插入时,它发送前两行以及第三行,依此类推.

I am running into problem, where when an Insert is completed successfully and if i continue to insert another row, in the next insert it is also sending the row that was inserted successfully earlier, so it goes like this. On the First insert that row is posted back to webAPI and inserted successfully. On Next Insert Two rows are sent one of them was from first step. On third Insert it send previous two rows as well as third row and so on.

这可能是什么原因?

这是有问题的代码.

$(document).ready(function () {
try {

var degreeYearsArray = new Array();

function GetDegreeName(dgID, degreeName) {
    for (var i = 0; i < degreeYearsArray.length; i++) {
        if (degreeYearsArray[i].dgID_PK == dgID) {
            return degreeYearsArray[i].Name;
        }
    }
    return degreeName;
}

var degreeYearModel = {
    id: "DGYR_PK",
    fields: {
        DGID_FK: {
            type: "number",
            nullable: false,
            editable: false
        },
        Code: {
            type: "string",
            validation: {
                required: true,
                minlength: 2,
                maxlength: 160
            }
        },
        Year: {
            type: "number",
            validation: {
                required: true
            }
        },
        EffectiveDate: {
            type: "date",
            validation: true

        },
        TerminationDate: {
            type: "date",
            validation: true
        }
    }
}; 




var baseURL = "http://localhost:3103/api/Degree";
var degreeYearTransport = {
    create: {
        url: baseURL + "/PostDegreeYears", // "/PutOrgSchool",
        type: "POST",
        // contentType: "application/json"
        dataType: "json"
    },
    read: {
        url: function () {
            var newURL = "";
            if (window.SelectedDegree == null)
                newURL =   baseURL + "/GetDegreeYears"
            else
                newURL =  baseURL + "/GetDegreeYears?degreeID=" + window.SelectedDegree.DGID_PK;
            return newURL;
        },
       dataType: "json" // <-- The default was "jsonp"
    },
    update: {
        url: baseURL + "/PutDegreeYears", //"/PostOrgSchool",
        type: "PUT",
        // contentType: "application/json",
        dataType: "json"
    },
    destroy: {
        url: function (employee) {
            return baseURL + "/deleteOrg" + employee.Id
        },
        type: "DELETE",
        dataType: "json",
        contentType: "application/json"
    },
    parameterMap: function (options, operation) {
        try {


            if (operation != "read") {
                options.EffectiveDate = moment(options.EffectiveDate).format("MM-DD-YYYY");
                options.TerminationDate = moment(options.TerminationDate).format("MM-DD-YYYY")
            }
            var paramMap = kendo.data.transports.odata.parameterMap(options);

            delete paramMap.$format; // <-- remove format parameter.

            return paramMap;

        } catch (e) {
            console.error("Error occure in parameter Map or Degree.js" + e.message);
        }

    }
}; //transport




var dsDegreeYears = new kendo.data.DataSource({
    serverFiltering: true, // <-- Do filtering server-side
    serverPaging: true, // <-- Do paging server-side
    pageSize: 2,
    transport: degreeYearTransport,
    requestEnd: function (e) {
        try {
            if (e.type == "update"){
                $.pnotify({
                    title: 'Update Sucessful',
                    text: 'Record was Updated Successfully',
                    type: 'success'
                });
            }

            if (e.type == "create") {
                $.pnotify({
                    title: 'Insert Sucessful',
                    text: 'Record was Inserted Successfully',
                    type: 'success'
                });
            }
        } catch (e) {
            console.error("error occured in requestEnd of dsDegreeYears datasource in DegreeYears.js" + e.message);
        }

    },
    schema: {
        data: function (data) {
            return data.Items; // <-- The result is just the data, it doesn't need to be unpacked.
        },
        total: function (data) {
            return data.Count; // <-- The total items count is the data length, there is no .Count to unpack.
        },
        model: degreeYearModel
    }, //schema
    error: function (e) {
        var dialog = $('<div></div>').css({ height: "350px", overflow: "auto" }).html(e.xhr.responseText).kendoWindow({
            height: "300px",
            modal: true,
            title: "Error",
            visible: false,
            width: "600px"

        });

        dialog.data("kendoWindow").center().open();
    },

});


$("#" + gridName).kendoGrid({
    dataSource: dsDegreeYears,
    autoBind: false,
    groupable: true,
    sortable: true,
    selectable: true,
    filterable: true,
    reorderable: true,
    resizable: true,
    columnMenu: true,
    height: 430,
    editable: "inline",
    toolbar: ["create"],
    pageable: {
        refresh: true,
        pageSizes: true,
        buttonCount: 5
    },
    columns: [  {
                    field: "DGID_FK",
                    title: "Degree Name",
                    width: 140,
                    template: function (dataItem) {
                        if (window.SelectedDegree != null) {
                            dataItem.DGID_FK = window.SelectedDegree.DGID_PK;
                            return window.SelectedDegree.Name;
                        }
                        else
                            return "";
                    }
                },
                {
                    field: "Code",
                    title: "Code",
                    width: 140
                },
                 {
                     field: "Year",
                     title: "Year",
                     width: 140
                 },
               {
                   field: "Description",
                   width: 110
               },

            {
                field: "EffectiveDate",
                width: 110,
                format: "{0:MM/dd/yyyy}"
            },
            {
                field: "TerminationDate",
                width: 110,
                format: "{0:MM/dd/yyyy}"
            },

             {
                 command: ["edit"] , title: "&nbsp;", width: "172px"
             }
    ]
}); //grid

//Hide history pull-down menu in the top corner
$.pnotify.defaults.history = false;
$.pnotify.defaults.styling = "bootstrap";
// styling: 'bootstrap'
//styling: 'jqueryui'


} catch (e) {
    console.error("Error occured in DegreeYears" + e.message);
}

});//文档

这是从 WebAPI 发送的响应

This is the Response that is sent from WebAPI

 {"$id":"1","DGYR_PK":27,"DGID_FK":64,"Year":4,"Code":"4THYR","EffectiveDate":"2014-01-11T00:00:00","TerminationDate":"2014-01-11T00:00:00","CreatedByUserID_FK":1,"DateCreated":"0001-01-01T00:00:00","UpdatedByUserID_FK":1,"DateUpdated":"0001-01-01T00:00:00","RowStatusID_FK":1,"Degree":null,"DegreeYearExamSchedules":[],"User":null,"RowStatu":null,"User1":null,"DegreeYearSubjects":[]}

所以我确实看到我正在返回用户在回答问题时建议的 ID.还在犹豫

So i do see i am returning ID as suggested by the users in response to the question. still wondering

推荐答案

您的主键不能为 0 或 null.如果您使用自动递增值,那么您应该在发布后调用数据源的重新读取"​​.检查值并确保您的数据值 > 0.注意:我已将模型中 PK 的默认值设置为列模型中的 -1 以帮助解决此问题.

Your primary key cannot be 0 or null. If you are using auto-incrementing values then you should invoke the "re-read" of your dataSource after post. Check the values and make sure your data values are >0. Note: I have set the default value of the PK in the model to -1 in the column model to help with this.

这篇关于Kendo UI Grid 再次发回已插入的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆