无法将 class 从 angularjs 传递到网络 api
unable to pass class from angularjs to web api
我正在尝试通过单击 WEB API 更新方法中的更新按钮来更新项目详细信息。
$scope.Update = function () {
var ItemData = {
ITEM_ID: $scope.inpItemId,
ITEM_NAME: inpItemName,
NET_WEIGHT: inpNetWeight,
};
//if (ItemData.ITEM_ID != null) {
// $http.put(
// 'http://localhost:55762/api/ItemMaintenance/UpdateItemDetails',
// // JSON.parse(JSON.stringify(ItemData)),
// JSON.stringify(ItemData),
// {
// headers: { 'Content-Type': 'application/json' }
// // body: JSON.stringify(ItemData);
// }
// ).success(function (response) {
// alert("Updated successfully....");
// }, function errorCallback(response) {
// alert("NOT ... Updated ....");
// });
if (ItemData.ITEM_ID != null || ItemData.ITEM_ID === "") {
$http({
method: 'put', url: 'http://localhost:55762/api/ItemMaintenance/UpdateItemDetails',
data: JSON.stringify(ItemData),
contentType: "application/json"
}).then(function successCallback(response) {
alert("Updated successfully....");
}, function errorCallback(response) {
alert("NOT ... Updated ....");
});
}
}
WEB API: 能够获取调试点 UpdateItemDetails 方法。
[HttpPut]
public HttpResponseMessage UpdateItemDetails([FromBody]MA_Item ItemDetails) //----ItemDetails are always null here
{
if (ItemDetails != null)
{
bool result = itemRepository.UpdateItemDetails(ItemDetails);
if (result)
return Request.CreateResponse(HttpStatusCode.OK, "Updated Successfully");
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something wrong !");
}
到达 WEB API 时,ItemDetails 始终为 null。
Ma_Item.cs 文件:
public class MA_Item
{
public string ITEM_ID { get; set; }
public string ITEM_NAME { get; set; }
public decimal NET_WEIGHT { get; set; }
......
}
如果您在 data
属性中发送 ItemData
,则无需在您的网络 api 操作中使用 [FromBody]
。从 -
By default web api (and asp.net mvc) in a POST request deserializes (reference) object arguments from http message body (data)
此外,由于您没有 post 您的 ItemDetails
class 看起来如何 - 您需要确保属性名称相似,以便在反序列化时可以正确映射它们。这也可能是将 ItemDetails
设为 NULL
的原因。一个好的约定是 -
在 JS 中最终使用 camelCase-
var ItemDetails = {
itemId: $scope.inpItemId,
itemName: inpItemName,
netWeight : inpNetWeight,
...
...
};
在 C# 端使用 PascalCase-
Class ItemDetails
{
public int ItemId {get; set;}
public string ItemName {get; set;}
public double NetWeight {get; set;}
}
我正在尝试通过单击 WEB API 更新方法中的更新按钮来更新项目详细信息。
$scope.Update = function () {
var ItemData = {
ITEM_ID: $scope.inpItemId,
ITEM_NAME: inpItemName,
NET_WEIGHT: inpNetWeight,
};
//if (ItemData.ITEM_ID != null) {
// $http.put(
// 'http://localhost:55762/api/ItemMaintenance/UpdateItemDetails',
// // JSON.parse(JSON.stringify(ItemData)),
// JSON.stringify(ItemData),
// {
// headers: { 'Content-Type': 'application/json' }
// // body: JSON.stringify(ItemData);
// }
// ).success(function (response) {
// alert("Updated successfully....");
// }, function errorCallback(response) {
// alert("NOT ... Updated ....");
// });
if (ItemData.ITEM_ID != null || ItemData.ITEM_ID === "") {
$http({
method: 'put', url: 'http://localhost:55762/api/ItemMaintenance/UpdateItemDetails',
data: JSON.stringify(ItemData),
contentType: "application/json"
}).then(function successCallback(response) {
alert("Updated successfully....");
}, function errorCallback(response) {
alert("NOT ... Updated ....");
});
}
}
WEB API: 能够获取调试点 UpdateItemDetails 方法。
[HttpPut]
public HttpResponseMessage UpdateItemDetails([FromBody]MA_Item ItemDetails) //----ItemDetails are always null here
{
if (ItemDetails != null)
{
bool result = itemRepository.UpdateItemDetails(ItemDetails);
if (result)
return Request.CreateResponse(HttpStatusCode.OK, "Updated Successfully");
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something wrong !");
}
到达 WEB API 时,ItemDetails 始终为 null。
Ma_Item.cs 文件:
public class MA_Item
{
public string ITEM_ID { get; set; }
public string ITEM_NAME { get; set; }
public decimal NET_WEIGHT { get; set; }
......
}
如果您在 data
属性中发送 ItemData
,则无需在您的网络 api 操作中使用 [FromBody]
。从
By default web api (and asp.net mvc) in a POST request deserializes (reference) object arguments from http message body (data)
此外,由于您没有 post 您的 ItemDetails
class 看起来如何 - 您需要确保属性名称相似,以便在反序列化时可以正确映射它们。这也可能是将 ItemDetails
设为 NULL
的原因。一个好的约定是 -
在 JS 中最终使用 camelCase-
var ItemDetails = {
itemId: $scope.inpItemId,
itemName: inpItemName,
netWeight : inpNetWeight,
...
...
};
在 C# 端使用 PascalCase-
Class ItemDetails
{
public int ItemId {get; set;}
public string ItemName {get; set;}
public double NetWeight {get; set;}
}