【发布时间】:2019-08-05 04:32:06
【问题描述】:
我正在尝试通过单击 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 始终为空。
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; }
......
}
【问题讨论】:
-
您是说您将请求参数设置为“NULL”吗?!
-
ItemDetails 未到达 Web API,并且始终为空。所以无法更新商品详情。
标签: javascript angularjs json asp.net-web-api http-put