如何使用apiController和angular删除多个记录
我试过以下方法。但我没有成功
api控制器:
public class NewsCategoriesController : ApiController
{
private readonly IJN_NewsCategoriesService _ijnNewsCategoriesService;
public NewsCategoriesController(IJN_NewsCategoriesService ijnNewsCategoriesService)
{
_ijnNewsCategoriesService = ijnNewsCategoriesService;
}
public void Delete(int id)
{
_ijnNewsCategoriesService.DeleteNewsCategory(id);
}
public void ManyDelete(DeleteViewModel ids)
{
var d = ids;
}
}
public class DeleteViewModel
{
public int[] ids { get; set; }
}
}
ajax方法:
deleteNewsCategories: function (id) {
return $http({
method: 'DELETE',
url: '/api/newsCategories/' + id
});
},
deleteManyNewsCategories: function (ids) {
return $http({
method: 'DELETE',
url: '/api/newsCategories/' + JSON.stringify(ids),
traditional: true
});
}
删除记录工作正常。如果要删除多条记录,会出现以下错误:
Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost:25768/api/newsCategories/[12,26]
问题解决了。
public void Delete(int id)
{
_ijnNewsCategoriesService.DeleteNewsCategory(id);
}
public void DeleteModels(DeleteViewModel dvm)
{
var d = dvm;
}
public class DeleteViewModel
{
public int[] Ids { get; set; }
}
$http
methods for delete:
deleteNewsCategories: function (id) {
return $http({
method: 'DELETE',
url: '/api/newsCategories/' + id
});
},
deleteManyNewsCategories: function (ids) {
return $http({
headers: {
'Content-type': 'application/json'
},
method: "DELETE",
url: "/api/newsCategories/",
data: { Ids: ids }
});
}