I'm trying to do a POST request with a JSON object as the body. The JSON object contains nested types that are predefined models in a library I am using, but when I use [FromBody] attribute only the root model gets binded while the nested models are null.
I've tried without using the [FromBody] attribute, but it also only binds the root level model.
POST Object Example: Foo would be a model that takes a Bar object. Bar would be a model that has properties name and firstLetter.
{
"foo": [
{
"bar": {
"name": "bar",
"firstLetter": "b"
}
},
{
"bar": {
"name": "bar1",
"firstLetter": "b"
}
}
]
}
The Controller route looks like:
[HttpPost("example-route")]
public async Task<ActionResult<string>> Static([FromBody]Request request){
//Some Action
}
The Request class would look like:
//Request class
public class Request{
[JsonConstructor]
public Request(Bar b){
this.Bar = b;
}
public List<Bar> Bar = { get; set; }
}
//Bar class
public class Bar {
public Bar(string name, string firstLetter){
this.Name = name;
this.FirstLetter = firstLetter;
}
public string Name { get; set; }
public string FirstLetter { get; set; }
}
When I'd call this, Bar will get assigned, but its properties of Name and FirstLetter will still be null.
Edit:I'll add the List in the example, but I may have oversimplified.The actual request looks more like:
{
"prop1": "field1",
"prop2": "4",
"prop3": {
"userId": "2",
"customerId": "4",
"type": {
"userType": "Manager",
"contactInfo": [
{
"system": "email",
"value": "test@test.com"
},
{
"system": "phone",
"value": "555-555-5555"
}
]
}
}
}
Where prop1, prop2, prop3, type and contactInfo are all models defined in a library I'm using. I'm trying to get the ContactInfo object, and so far it can assigns two objects to ContactInfo when I step through, but their properties (system and value) are both null. I checked spelling and casing, but no issues there.