user3428422
user3428422

Reputation: 4570

AJAX send JSON array not binding in controller

I have an issue that my controller is not receiving the JSON array I send via AJAX as it's not binded to a model/ViewModel

This is what I am sending

enter image description here

You can see the array has a string id and text. Therefore I have tried adding my own model with the properties in:

enter image description here

And then trying to catch the array in the controller:

enter image description here

However you can see it's coming in the POST as NULL.

In Chrome -> Dev tools -> Network tab. The Form Data is

enter image description here

What am I missing?

Thanks

Upvotes: 2

Views: 884

Answers (1)

Nkosi
Nkosi

Reputation: 247433

An array is posted but the action expects a single object.

Also for model binding from the body of the request you can use [FromBody] attribute

[HttpPost]
public IActionResult InsertMasterTemplate([FromBody]headingstree[] tree) {
    //...
}

Reference Model Binding in ASP.NET Core

ensuring to include:

contentType: "application/json; charset=utf-8",`

in the AJAX method,

Upvotes: 3

Related Questions