I'm building an ASP.NET Core Web API endpoint that needs to receive an array of complex objects via multipart/form-data
. Each object includes key-value string pairs and an optional file upload. Each FormItem
must have either a value or a file, but not both, and not neither. Exactly one must be provided.
Here's my model:
public class FormItem
{
public string key { get; set; }
public string value { get; set; }
public IFormFile file { get; set; }
}
And my controller action looks like this:
[HttpPost]
public async Task<ActionResult> SaveAdditionalInfo(
[FromForm] FormItem[] formFields,
[FromRoute] Guid businessKycId,
[FromQuery] string token)
{
...
}
❓ What I want:
- To receive an array of
FormItem
via amultipart/form-data
POST
request. - To support both key-value pairs and file uploads.
- To support it in Swagger UI (using Swashbuckle) as well.
❌ Problem:
- Model binding does not populate the
formFields
correctly when submitted from Swagger or Postman. - Swagger UI doesn't render this correctly either — no way to add multiple
FormItem
entries with files.
How can I properly receive an array of complex objects (with IFormFile
) from a form post in ASP.NET Core?
Is there a recommended way to make this work with both model binding and Swagger UI?