[英]WebApi2 how to pass Json with an array
I have the following Json with array that I am sending it from AngularJS
我有以下Json与我从
AngularJS
发送的数组
{id_fleet: 4177, id_fleetfeature: Array[2]}
var dataFeatures = {
id_fleet: $scope.id,
id_fleetfeature: $scope.selectedFeatures
}
$http.put(myUrl + "Fleets/Insert", JSON.stringify(dataFeatures)).then(function () {
Materialize.toast('Features insertadas correctamente', 3000);
$scope.refreshData();
});
Web API Model Web API模型
public class modelFleetFeatures {
public int id_fleet { get; set; }
public int[] id_fleetfeature { get; set; }
}
Web API Function Web API功能
[HttpPut]
[Route("api/Fleets/FleetFeature/Update")]
public HttpResponseMessage updateFleetFeature(List<modelFleetFeatures> data)
{
var cnt = 0;
foreach (var item in data)
{
db.sp_Fleet_FleetFeaturesDelete(item.id_fleet);
db.SaveChanges();
}
foreach (var item in data)
{
db.sp_Fleet_FleetFeaturesInsert(item.id_fleet, item.id_fleetfeature[cnt]);
db.SaveChanges();
cnt +=1;
}
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
In the Web API
the value of data always gets null 在
Web API
中,数据值始终为null
You are sending single object in JSON, so why expecting list of object in API action? 您正在以JSON发送单个对象,那么为什么要在API操作中期待对象列表呢? Just expect single object in api Action , you will get that object model in call like :
只期望api Action中有单个对象,您将在调用中获得该对象模型,例如:
public HttpResponseMessage updateFleetFeature(modelFleetFeatures data)
{
//do stuff
}
And no need to stringify json object in put request. 并且无需在放置请求中对json对象进行字符串化。
$http.put(myUrl + "Fleets/Insert", dataFeatures).then(function () {
Materialize.toast('Features insertadas correctamente', 3000);
$scope.refreshData();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.