Dani
Dani

Reputation: 1

Why ajax passing null or 0 value to controller?

var newRecords = [];

values: [
  {
    "id": 7,
    "name": "Raddish",
    "rate": 30,
    "weight": "5",
    "amountperweight": 150
  },
  {
    "id": 8,
    "name": "Peas",
    "rate": 35,
    "weight": "6",
    "amountperweight": 210
  }
]

$.ajax({
            method: "post",
            url: "http://localhost:36551/Orders/GenerateOrder",
            data: { "values": newRecords },
            dataType: "json",
            success: function (response) {
            },
            error: function (error) {
            }
        });


    [HttpPost]
    [Route("GenerateOrder")]
    [ActionName("GenerateOrder")]
    public List<OrderCart> GenerateOrder(List<OrderCart> generateOrder)
    {
        return generateOrder;
    }

Upvotes: -2

Views: 474

Answers (1)

Markuzy
Markuzy

Reputation: 505

On your javascript, make sure also that newRecords is equals to the following like this before you even call the ajax post.

newRecords = [{ "id": 7, "name": "Raddish", "rate": 30, "weight": "5", "amountperweight": 150 }, { "id": 8, "name": "Peas", "rate": 35, "weight": "6", "amountperweight": 210 }];

Your js property name does not correspond directly to your controller action's property name. On your javascript block, change your

data: { "values": newRecords },

to

data: { "generateOrder": newRecords },

Upvotes: 0

Related Questions