我正在使用WebApi和Knockout.js的ASP.NET MVC 5项目,我正在尝试在页面上保存时替换集合。首先,对于在我的WebApiController中工作的简单事物,更新一个项目:
public IHttpActionResult Put(FooObject obj, CancellationToken cancellationToken) {
// obj is of type FooObject and has the goods I need
...
}
然而,这不起作用:
public IHttpActionResult Put(IEnumerable<FooObject> listOfObj, CancellationToken cancellationToken) {
// nope; listOfObj is empty, including trying List<> instead of IEnumerable<>
...
}
这是我的相关JS代码:
draggable_service.prototype.update = function (data) {
var self = this;
return $.ajax({
data: data,
type: "PUT",
url: self.url
});
};
...
var draggableService = new draggable_service(service_url);
...
function submitChanges() {
draggableService.update(vm.addedItems());
}
...
我的直觉是我缺少一些基本的WebApi东西,但我不确定那是什么。有什么想法吗?
答案 0 :(得分:1)
我认为你必须使用frombody属性
标记你的集合参数public IHttpActionResult Put([FromBody]IEnumerable<FooObject> listOfObj, CancellationToken cancellationToken) {
// nope; listOfObj is empty, including trying List<> instead of IEnumerable<>
...
}
因为我重新调用ajax调用的数据应采用以下格式:
draggable_service.prototype.update = function (data) {
var self = this;
return $.ajax({
data: {'': data},
type: "PUT",
url: self.url
});
};