1

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 a multipart/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?

2

1 Answer 1

0

Change formFields to be IEnumerable<FormItem>. The default router and json parser doesn't see a json array as a .NET array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.