我在asp mvc解决方案下有下一个方法Odata方法,这个方法100%由向导生成,仅用于我的学习案例。
[AcceptVerbs("PATCH", "MERGE")]
public async Task<IHttpActionResult> Patch([FromODataUri] long key, Delta<SchoolChildrens> patch)
{
Validate(patch.GetEntity());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
SchoolChildrens schoolChildrens = await db.SchoolChildrensSet.FindAsync(key);
if (schoolChildrens == null)
{
return NotFound();
}
patch.Patch(schoolChildrens);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SchoolChildrensExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(schoolChildrens);
}
我有下一个Javascript代码来调用这个方法:
self.updateItem = function (data) {
var student = ko.toJS(data);
$.ajax({
url: '/odata/SchoolChildrens(' + student.Id + ')',
data: JSON.stringify(student),
type: 'PATCH',
success: function () {
},
error: function () {
},
xhr: function () {
return window.XMLHttpRequest == null || new window.XMLHttpRequest().addEventListener == null ? new window.ActiveXObject("Microsoft.XMLHTTP") : $.ajaxSettings.xhr();
}
});
};
我的记录从未通过此方法更新。 如何正确使用它?我该如何发送Delta?
答案 0 :(得分:1)
是否可以添加:
$.ajax({
...
contentType: 'application/json; charset=utf-8',
datatype: 'json',
...
};