I want to send a list of integers inside an object to a POST API in C#. This is what I am doing:
const headers = new HttpHeaders().append(
"Content-Disposition",
"multipart/form-data"
);
let formData: FormData = new FormData();
formData.append("name", "name");
formData.append("Ids", JSON.stringify(Ids));
return this._http.post(
`${environment.baseUrl}${this.companyApiUrl}`,
formData,
{ headers: headers }
);
Where Ids is of type number[].
The following is the C# WebAPI:
public ActionResult<MyModel> Post([FromForm] MyModel model)
{
...
}
MyModel :
public class MyModel
{
public string Name { get; set; }
public List<int> Ids { get; set; } // I have also tried int[]
}
I am getting the following error from the Web API:
{"Ids":["The value '[5,6,7]' is not valid."]}
How do I achieve this? I want to keep the type of Ids in MyModel as List<int> or int[] (directly send the Ids without JSON parsing in the server).