4

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

我的网格是:

 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);
    }

你有什么想法吗?我在哪里做错了什么?提前致谢。PS控制器中的查询工作正常。

4

1 回答 1

3

我解决了这个问题,按照 OnaBai 的返回更新/创建实体的建议,在删除的情况下,我返回了已删除条目的 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);
    }

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

于 2013-07-29T10:21:24.557 回答