我试图将Kendo调度程序与一组ASP.NET MVC Web API一起使用。客户端作为Angular JS应用程序执行。服务器端:用于读取我使用odata控制器创建/更新/删除操作Web API控制器。
我有三个配置调度程序的问题:
当我尝试为create&发送模型时更新它不会发送一个模型,但发送一捆它
{" models":[{" Id":0," Title":" No title"," StartDate& #34;:" 2015-04-28T11:00:00.000Z""结束日期":" 2015-04-28T11:30:00.000Z",& #34; StartTimezone":""" EndTimezone":"""说明":"&# 34;," RecurrenceID":0," RecurrenceRule":""" RecurrenceException":"&#34 ;, " IsAllDay":false," OwnerID":0},{" Id":0,"标题":"没有标题& #34;"起始日期":" 2015-04-28T13:00:00.000Z""结束日期":" 2015-04-28T13: 30:00.000Z"" StartTimezone":""" EndTimezone":"""描述&# 34;:""" RecurrenceID":0," RecurrenceRule":""" RecurrenceException&#34 ;: """ IsAllDay":假," OWNERID":0}]}
我的更新方法将数据发送到" api / Schedule / CreateEvent",而不是正确的方式" api / Schedule / UpdateEvent"
我的删除方法将数据发送到" api / Schedule / CreateEvent"而不是正确的方式" api / Schedule / DeleteEvent /"
有人可以帮助我吗?
Angular Code:
$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'];
}
}
}
};
Web 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
}
}
答案 0 :(得分:1)
发送多个模型的原因可能是因为数据源的批处理模式设置为true;如果你改变这个
batch: true,
到这个
batch: false,
它应该单独发送创建/更新(有关详细信息,请参阅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
。
至于其他问题 - 需要更多信息,请参阅我发布的评论 - 对于这些问题,您可能会有更好的解决方案:)