简体   繁体   中英

cant bind parameters to .net core

getting nulls in the .net core method trying to send array of object. I tried changing model but it won't seem to bind correctly

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'
        }
    }).

and in .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; }
}

Try:

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'
        }
    }).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM