2

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

5
  • In Chrome Dev Tools, can you see the json payload in the Network tab? Commented Oct 5, 2018 at 15:54
  • Funny that, I probably was doing that as you wrote the message.. In the Network section under headers, the Form Data is "Undefined" Commented Oct 5, 2018 at 15:56
  • Scrap that, its because I took out JSON.stringify Commented Oct 5, 2018 at 16:03
  • you send an array but the action expects a single object. Commented Oct 5, 2018 at 16:04
  • @Nkosi even with List<headingstee> it comes back as NULL Commented Oct 5, 2018 at 16:07

1 Answer 1

3

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,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.