Reputation: 4570
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
You can see the array has a string id and text. Therefore I have tried adding my own model with the properties in:
And then trying to catch the array in the controller:
However you can see it's coming in the POST as NULL.
In Chrome -> Dev tools -> Network tab. The Form Data is
What am I missing?
Thanks
Upvotes: 2
Views: 884
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