[英]unable to pass class from angularjs to web api
I'm trying to update Item details as below on a click of an update button in WEB API update method. 我试图通过单击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: able to get debug point UpdateItemDetails method. 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 !");
}
ItemDetails are always null when reaching WEB API. 到达WEB API时,ItemDetails始终为null。
Ma_Item.cs file: 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; }
......
}
If you send ItemData
in data
attribute, then there's no need to use [FromBody]
in your web api action. 如果您在
data
属性中发送ItemData
,则无需在Web api操作中使用[FromBody]
。 From this answer - 从这个答案 -
By default web api (and asp.net mvc) in a POST request deserializes (reference) object arguments from http message body (data)
Also as you didn't post how your ItemDetails
class looks like - you need to make sure attribute names are alike so that they can be mapped correctly while deserializing. 同样,由于您没有发布
ItemDetails
类的外观,因此-您需要确保属性名称相同,以便在反序列化时可以正确映射它们。 This may also be a reason of getting ItemDetails
as NULL
. 这也可能是将
ItemDetails
获取为NULL
的原因。 One good convention is - In JS end use camelCase- 一个好的约定是-在JS最终使用camelCase-
var ItemDetails = {
itemId: $scope.inpItemId,
itemName: inpItemName,
netWeight : inpNetWeight,
...
...
};
In the C# end use PascalCase- 在C#端使用PascalCase-
Class ItemDetails
{
public int ItemId {get; set;}
public string ItemName {get; set;}
public double NetWeight {get; set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.