无法将参数绑定到.net核心

时间:2018-05-29 18:35:48

标签: c#

在尝试发送对象数组的.net核心方法中获取空值。我尝试改变模型,但似乎没有正确绑定

var dataObj = { OrderDetails: [] };
for (var i = 0; i < OrderUnits.length; i++) {
    if (OrderUnits[i].ProductID != null) {
        var DtoOrderDetails = {
            OrderID: OrderID,
            ProductID: OrderUnits[i].ProductID,
            Qty: OrderUnits[i].Quantity
        };
        dataObj.OrderDetails.push(DtoOrderDetails );
    }
}
if (dataObj.OrderDetails.length > 0) {
    $http({
        method: "POST",
        url: "",
        data: JSON.stringify(dataObj),
        headers: {
            'Content-Type': 'application/json'
        }
    }).

和.net core

   [HttpPost]
        [ActionName("AddOrderDetails")]
        public string AddOrderDetails([FromBody] AddOrderDetailsReq[] OrderDetails)


public class AddOrderDetailsReq
{
    public int? OrderID { get; set; }

    private int? ProductID { get; set; }

    private int? Qty { get; set; }
}

1 个答案:

答案 0 :(得分:0)

尝试:

var orderDetails = [];
for (var i = 0; i < OrderUnits.length; i++) {
    if (OrderUnits[i].ProductID != null) {
        var DtoOrderDetails = {
            OrderID: OrderID,
            ProductID: OrderUnits[i].ProductID,
            Qty: OrderUnits[i].Quantity
        };
        orderDetails.push(DtoOrderDetails );
    }
}
if (orderDetails.length > 0) {
    $http({
        method: "POST",
        url: "",
        data: JSON.stringify(orderDetails),
        headers: {
            'Content-Type': 'application/json'
        }
    }).