Kendo UI Grid多次触发CRUD操作

时间:2013-07-26 14:05:50

标签: grid kendo-ui crud

我在很多地方都看到过这个问题,但我似乎无法找到解决方案。因此,我已经使用CRUD操作定义了一个Kendo网格,事实是先前被解雇的操作再次被触发。     假设您删除条目X然后添加条目Y,则启动创建操作,然后再次触发删除操作(对于X - 已删除)。如果您首先创建一个元素然后编辑另一个元素,它会编辑第二个元素,然后重新触发第一个create语句并为第一个插入元素插入一个副本。如果您继续进行多项操作,则会发生噩梦,所有其他先前的操作都会被触发并发送给控制器。

我的网格是:

 function InitializeIPAddressesGrid(userID) {
    selectedUserID = userID;
    $(".ipAddresses").kendoGrid({
        dataSource: IPAdressesDataSource,
        sortable: {
            mode: "single",
            allowUnsort: false
        }, 
        remove: function (e) {
            this.refresh();
            var canDelete = confirm("Are you sure you want to delete this record?");
            if (!canDelete) {
             e.preventDefault();
             }

        },
        height: 420,
        resizable: true,
        pageable: {
            refresh: true,
            pageSize: 10
        },
        selectable: "row",
        toolbar: ["create"],
        editable:{mode: "inline", confirmation:false} ,
        columns: [{
            field: "IpAddress",
            title: "IP Address"
        },
        {
            field: "Status",
            title: "Status"
        },
        {
            field: "LockedUntil",
            title: "Locked until",
            template: "#=kendo.toString(LockedUntil, 'yyyy/MM/dd' )#" 
        },
           { command: ["edit", "destroy"], title: " ", width: "180px" }
        ]
    });

}
var IPAdressesDataSource = new kendo.data.DataSource({
    type: "json",
    serverPaging: true,
    serverSorting: true,
    serverFiltering: true,
    pageSize: 10,
    //scrollable:false, 
    transport: {
        read: {
            url: websiteRootUrl + '/PortalAuthorization/GetIPAddressesList',
        },
        update: {
            url: websiteRootUrl + "/PortalAuthorization/UpdateIP",
            dataType: "json",
            type: 'POST', 
            complete: function (e) {
                            if (e.status != 200) {
                                alert(eval('(' + e.responseText + ')').Message);
                            }
                            }
            },
        create:  {
              url: websiteRootUrl + "/PortalAuthorization/CreateIP",
              dataType: "json",
              type: 'POST',
               complete: function (e) {
                                if (e.status != 200) {
                                    alert(eval('(' + e.responseText + ')').Message);

                                }
                                }
              },
        destroy: {
             url: websiteRootUrl + "/PortalAuthorization/DeleteIP",
            dataType: "json",
            type: 'DELETE',
            complete: function (e) {
                            if (e.status != 200) {
                                alert(eval('(' + e.responseText + ')').Message);
                            }
                            }
        },

        parameterMap: function (options, operation) {

            if (operation == "update" && options) {
                return {ipAddress: options.IpAddress , 
                        status: options.Status ,
                        lockedUntil: kendo.toString(options.LockedUntil, 'yyyy/MM/dd' ),
                        pkey: options.ID,
                        databaseID: selectedDatabaseID };
            }
            else
            if (operation == "destroy" && options)
            {
                 return {
                        databaseID: selectedDatabaseID,
                        pkey: options.ID,
                        userIDParam: selectedUserID
                         };   
            }
            else
             if (operation == "create" && options) {
                return {ipAddress: options.IpAddress , 
                        status: options.Status ,
                        lockedUntil: kendo.toString(options.LockedUntil, 'yyyy/MM/dd' ),
                        pkey: options.ID,
                        userIDParam: selectedUserID,
                        databaseID: selectedDatabaseID };
            }
            else
            {
            options.databaseID = selectedDatabaseID;
            options.userID = selectedUserID;
            return options;
            }
        }
    },
    schema: {
        model: {
                 id: "ID",
                 fields: {
                        IpAddress: { type: "string" },
                        Status: { type: "string" },
                        LockedUntil: { type: "date" }
                 }
             },      
        data: function (data) {
            return data.Items;
        },
        total: function (data) {
            return data.TotalCount;
        }
    }
});

我的控制器是:

 public object UpdateIP(int databaseID, long pkey, string status, string lockedUntil, string ipAddress)
    {
          var database = [...];
        DynamicDataRepository repository = [...];
        string query = "...";

           repository.ExecuteNonQuery(query);

        return new HttpResponseMessage(HttpStatusCode.OK);
    }

    public object DeleteIP(int databaseID, long pkey, int? userIDParam)
    {
         var database = [...];
        DynamicDataRepository repository = [...];
        string query = "...";

           repository.ExecuteNonQuery(query);

        return new HttpResponseMessage(HttpStatusCode.OK);
    }

    public object CreateIP(int databaseID, long? pkey, string status, string lockedUntil, string ipAddress, int? userIDParam)
    {
        var database = [...];
        DynamicDataRepository repository = [...];
        string query = "...";

           repository.ExecuteNonQuery(query);

        return new HttpResponseMessage(HttpStatusCode.OK);
    }
你有什么想法吗?我做错了什么?提前致谢。附:控制器中的查询工作正常。

1 个答案:

答案 0 :(得分:3)

我修复了问题,按照OnaBai的建议返回Updated / Created实体,在删除的情况下,我返回了已删除条目的ID。

 public object UpdateIP(int databaseID, long pkey, string status, string lockedUntil, string ipAddress)
    {
        var database = [...];
        DynamicDataRepository repository = [...];
        string query = [...];

        IPList updatedIP = new IPList { ID = pkey, IpAddress = ipAddress, Status = status, LockedUntil = DateTime.Today };

        return Json(updatedIP, JsonRequestBehavior.AllowGet);

       // return new HttpResponseMessage(HttpStatusCode.OK);
    }

只有一个提及:在CREATE的情况下,该方法似乎不起作用所以我所做的是在CREATE操作的.complete事件中我做了一个ipGrid.dataSource.read(); ipGrid.refresh(); - 所以操作不会重复。 (我读到在这种情况下,模型定义可能存在问题 - 设置ID字段 - 但我确实设置了那个)。非常感谢OnaBai