Not sure where I'm going wrong, I have an object that just won't get posted to my controller.
Model:
public class PostJsonModel
{
public List<JsonModel> Things { get; set; }
}
public class JsonModel
{
public int Id { get; set; }
public string Tests { get; set; }
public string MoreTests { get; set; }
}
Controller:
[HttpPost]
public async Task<IActionResult> DoSomething(PostJsonModel test)
{
//Save to the database
return Json("Success");
}
javascript:
var test = {
"Id": 5,
"Tests": "Testing",
"MoreTests": "More More More"
};
var more = [];
more.push(test);
more.push(test);
more.push(test);
var allSaveElements = {
"Things": more
};
let xhr = new XMLHttpRequest();
xhr.open('POST', '/Home/DoSomething', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
if (xhr.status === 200) {
//Do something
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send(allSaveElements);
When I break into the controller, the PostJsonModel has "Things" with a count of 0.
Looking at the network output in chrome, the allSaveElements is exactly what I expect it to be.
This works perfectly fine in a .NET Framework (4.7) application, but for some reason not in .NET Core (2.2)
I'm obviously missing something, can someone point it out?
public async Task<IActionResult> DoSomething([FromBody] PostJsonModel test)