Kendo 调度程序中的更新和销毁实体有什么问题

What's wrong with update & destroy entities in Kendo scheduler

我试图将 Kendo 调度程序与 ASP.NET MVC Web API 捆绑在一起使用。客户端作为 Angular JS 应用程序执行。服务器端:对于读取,我正在使用 odata 控制器进行 create/update/delete 操作 Web API 控制器。

我在调度程序配置方面遇到三个问题:

  1. 当我尝试发送模型进行创建和更新时,它不会发送一个模型,而是发送一捆模型

    {"models":[{"Id":0,"Title":"No title","StartDate":"2015-04-28T11:00:00.000 Z","EndDate":"2015-04-28T11:30:00.000Z","StartTimezone":"","EndTimezone":"","Description":"","RecurrenceID":0,"RecurrenceRule":"","RecurrenceException":"","IsAllDay":false,"OwnerID":0},{"Id":0 ,"Title":"No title","StartDate":"2015-04-28T13:00:00.000Z","EndDate":"2015-04-28T13:30:00.000Z" ,"StartTimezone":"","EndTimezone":"","Description":"","RecurrenceID":0,"RecurrenceRule":"","RecurrenceException" :"","IsAllDay":false,"OwnerID":0}]}

  2. 我的更新方法将数据发送到 "api/Schedule/CreateEvent",而不是正确的方式 "api/Schedule/UpdateEvent"

  3. 我的删除方法将数据发送到 "api/Schedule/CreateEvent",而不是正确的方式 "api/Schedule/DeleteEvent/"

有人可以帮助我吗?

Angular代码:

$scope.schedulerOptions = {
                date: new Date("2015/04/28"),
                dateHeaderTemplate: kendo.template("<strong>#=kendo.toString(date, 'd/M')#</strong>"),
                height: 600,
                views: [
                    //"day",
                    {
                        type: "workWeek",
                        selected: true,
                    },
                    //"week",
                    //"month",
                ],
                timezone: "Etc/UTC",
                dataSource: {
                    batch: true,
                    type: 'odata',
                    transport: {
                        read: {
                            url: "odata/ScheduleOData",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                        },
                        update: {
                            url: "api/Schedule/UpdateEvent",
                            type: "Post",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                        },
                        create: {
                            url: "api/Schedule/CreateEvent",
                            type: "Post",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                        },
                        destroy: {
                            url: function (data) {
                                return "api/Schedule/DeleteEvent/" + data.Id;
                            },
                            type: "Delete",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                        },
                        //parameterMap: function (data, operation) {
                        //    return JSON.stringify(data);
                        //}
                        parameterMap: function (data, operation) {
                            if (operation == "destroy") {
                                return;// kendo.stringify(data);
                            }
                            var d = kendo.data.transports.odata.parameterMap(data, operation);
                            delete d.$inlinecount; // <-- remove inlinecount parameter    
                            delete d.$callback;
                            return d;
                        }
                    },
                    schema: {
                        model: {
                            id: "Id",
                            fields: {
                                taskId: { from: "Id", type: "number" },
                                title: { from: "Title", defaultValue: "No title", validation: { required: true } },
                                start: { type: "date", from: "StartDate" },
                                end: { type: "date", from: "EndDate" },
                                startTimezone: { from: "StartTimezone" },
                                endTimezone: { from: "EndTimezone" },
                                description: { from: "Description" },
                                recurrenceId: { from: "RecurrenceID", defaultValue: 0 },
                                recurrenceRule: { from: "RecurrenceRule" },
                                recurrenceException: { from: "RecurrenceException" },
                                isAllDay: { type: "boolean", from: "IsAllDay" },
                                ownerId: { from: "OwnerID", defaultValue: 0 }
                            }
                        },
                        data: function (response) {
                            return response['value'];
                        },
                        total: function (response) {
                            return response['odata.count'];
                        }

                    }
                }
            };

网页API代码:

public class ScheduleController : ApiController {
        private readonly ISubjectsService _subjectsService;
        private readonly IGenericService _genericService;
        private readonly IMembershipService membership;
        private readonly IMappingEngine mapper;

        public ScheduleController(ISubjectsService subjectsService, IGenericService igs, IMappingEngine mapper, IMembershipService membership) {
            this._subjectsService = subjectsService;
            this._genericService = igs;
            this.mapper = mapper;
            this.membership = membership;
        }

        public void UpdateEvent(RootObject model) {
            // some logic
        }

        public void CreateEvent(RootObject model) {
            // some logic
        }

        [HttpDelete]
        public void DeleteEvent(int key) {
            // some logic
        }
    }

发送多个模型的原因可能是因为数据源的批处理模式设置为 true;如果你改变这个

 batch: true,

至此

 batch: false,

它应该单独发送 creates/updates(有关详细信息,请参阅 here

我不确定这是否是您所需要的,但是如果您想要发送包含子模型数组的 单个 模型,您可以这样做:

 parameterMap: function (data, operation) {

   if (operation=="create" || operation=="update"){
      var rootModel = {rootProperty:"foo",models:[]};
       data.models.forEach(function(model)){
           //do whatever transformation of the individual models here
           //and add to the aggregate model.
           rootModel.models.push(model);
       }
       return rootModel; //whatever you return from parameterMap is what will be sent in the HTTP request.
     }
     //rest of your special operation cases here                     
  }

在那种情况下,您可能确实希望保留 batch:true.

至于其他问题 - 需要更多信息,请参阅我发布的评论 - 你最好单独提出这些问题:)