sno*_*FFF 2 c# odata asp.net-web-api kendo-grid
我有一个Kendo UI Grid连接到odata CRUD服务(Web API 2.2 OData V4).dataSource配置如下所示 - baseUrl对所有人来说都是相同的,只是HTTP动词更改.
var dataSource = new kendo.data.DataSource({
type: "odata",
transport: {
read: {
beforeSend: prepareRequest,
url: baseUrl,
type: "GET",
dataType: "json"
},
update: {
beforeSend: prepareRequest,
url: function (data) {
return baseUrl + "(" + data.CategoryId + ")";
},
type: "PUT",
dataType: "json"
},
create: {
beforeSend: prepareRequest,
url: baseUrl,
type: "POST",
dataType: "json"
},
destroy: {
beforeSend: prepareRequest,
url: function (data) {
return baseUrl + "(" + data.CategoryId + ")";
},
type: "DELETE",
dataType: "json"
},
parameterMap: function (data, operation) {
if (operation == "read") {
var paramMap = kendo.data.transports.odata.parameterMap(data);
delete paramMap.$format;
delete paramMap.$inlinecount;
paramMap.$count = true;
return paramMap;
} else if (operation == "create" || operation == "update") {
delete data["__metadata"];
return JSON.stringify(data);
}
}
},
batch: false,
pageSize: 10,
serverPaging: true,
serverSorting: true,
serverFiltering: true,
sort: { field: "CategoryCode", dir: "asc" },
schema: {
data: function (data) { return data.value; },
total: function (data) { return data['@@odata.count']; },
model: {
id: "CategoryId",
fields: {
CategoryId: { editable: false, type: "number" },
CategoryCode: { editable: true, type: "string", required: true, validation: { maxlength: 2 } },
Description: { editable: true, type: "string", required: true, validation: { maxlength: 50 } },
Created: { editable: false, type: "date" },
CreatedBy: { editable: false, type: "string" },
Updated: { editable: false, type: "date" },
UpdatedBy: { editable: false, type: "string" }
}
}
},
error: function (e) {
commonNotification.hide();
commonNotification.show(getRequestError(e), "error");
},
change: function (e) {
commonNotification.hide();
if (e.action == "sync") {
commonNotification.show("@SharedResources.Changes_Saved", "success");
}
},
requestStart: function (e) {
if (e.type == "read" && this.hasChanges()) {
if (confirm("@SharedResources.Dirty_Navigation_Confirmation") == false) {
e.preventDefault();
} else {
this.cancelChanges();
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
一般来说,一切都很好.prepareRequest()函数用于应用自定义授权标头以及将Accept标头设置为"application/json; odata = verbose".
在阅读时,JSON响应类似于以下内容 - 这就是dataSource.schema.data函数返回data.value的原因
{
"@odata.context":"https://localhost:44305/odata/$metadata#Categories",
"@odata.count":2,
"value":[
{
"CategoryId":1,
"CategoryCode":"01",
"Description":"Something",
"Created":"2014-08-01T11:03:30.207Z",
"CreatedBy":"DOMAIN\\User",
"Updated":"2014-09-05T14:36:22.6323744-06:00",
"UpdatedBy":"DOMAIN\\User"
},{
"CategoryId":2,
"CategoryCode":"02",
"Description":"Something Else",
"Created":"2014-08-01T11:03:35.61Z",
"CreatedBy":"DOMAIN\\User",
"Updated":"2014-08-26T16:07:29.198241-06:00",
"UpdatedBy":"DOMAIN\\User"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,当我创建或更新实体时,返回的JSON如下所示:
{
"@odata.context":"https://localhost:44305/odata/$metadata#Categories/$entity",
"CategoryId":3,
"CategoryCode":"03",
"Description":"Yet Another",
"Created":"2014-09-06T07:55:52.4933275-06:00",
"CreatedBy":"DOMAIN\\User",
"Updated":"2014-09-06T13:55:34.054Z",
"UpdatedBy":""
}
Run Code Online (Sandbox Code Playgroud)
因为它没有被"值"包裹,所以Kendo网格没有正确更新数据源.执行POST或PUT的控制器当前返回实体,如下所示:
return Created(category);
Run Code Online (Sandbox Code Playgroud)
要么
return Updated(category);
Run Code Online (Sandbox Code Playgroud)
我能够通过更改对JsonResult的响应来解决问题,如下所示:
return Json(new { value = new[] { category } });
Run Code Online (Sandbox Code Playgroud)
有了它,一切都按预期工作......但是,我的HTTP响应现在是200(似乎JsonResult总是以200响应).在一个完美的世界里,我可以在创造时回归201.我是否应该接受我让它工作并与200一起生活,或者,是否有一种简单的方式来响应201并仍然根据需要格式化我的JSON?似乎Web API 2允许更自定义的http响应,但我的web api 2.2控制器操作正在返回IHttpActionResult.我真的不想创建一个自定义类只是为了有一个特殊的返回类型,我似乎无法使用Created()返回我的匿名对象.
总而言之,我真的倾向于生活在我拥有的东西中.但是,我会对使用201返回我的匿名对象的方法感兴趣,或者,在我的kendo dataSource中接受非"值包装"json的方法并让它适当地更新数据.
| 归档时间: |
|
| 查看次数: |
4410 次 |
| 最近记录: |