I’m trying to bind the JSON, that’s coming from a source I cannot control, to a custom class in a controller. The problem that I ran to is the fact that the array of values are sent in format [{‘value’: ‘value1’}, [{‘value’:’value2′}]. I know that I can create an object that would have one field ‘value’ and the binding would work, but I want to avoid that and get all the values from the request bound to a string list.
I have this controller method
public async Task<IActionResult> UploadData(List<DataModel> values)
And here’s how I’d like the DataModel to look like.
public class DataModel
{
public string? a{ get; set; }
public List<string>? b{ get; set; }
}
And here’s how the example of data
[{
"a": "name",
"b": [
{
"value": "one"
},
{
"value": "two"
}
]
}]
Is there a way to achieve this, preferably using an attriubte?
2
Answers
Leave the binding object exactly as it should be to map from the source you can’t control and just make a custom getter for the object that transforms it in a list like you want. Using automapper or other suggested third party libs will add an unnecessary layer of complexity.
then when you need the value around you just call
the simpliest way is to change the action input parameter type
another way is to use a JsonConstructor