【问题标题】:unable to pass class from angularjs to web api无法将类从 angularjs 传递给 web api
【发布时间】: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


【解决方案1】:

如果您在data 属性中发送ItemData,则无需在您的Web api 操作中使用[FromBody]。来自this answer -

By default web api (and asp.net mvc) in a POST request deserializes (reference) object arguments from http message body (data)

此外,由于您没有发布 ItemDetails 类的外观 - 您需要确保属性名称相似,以便在反序列化时可以正确映射它们。这也可能是将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;}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多