2

I have a nested model. When I make a post request with Postman I can get all attributes except the nested File attribute. Why do only nested file attribute comes null and what is the solution? The nested model and screenshots of the request are below:

public class RequestDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IFormFile Image { get; set; }
    public TestModel Test { get; set; }
}

public class TestModel
{
    public string Desc { get; set; }
    public IFormFile File { get; set; }
}

enter image description here enter image description here

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
emy
  • 664
  • 1
  • 9
  • 22
  • Square-brackets are for binding to string-keyed Dictionaries or `List` members of form classes - so a `name=""` like `Test[file]` would be if you had a `Dictionary` member in `RequestDTO`. Instead use `Test.File`. – Dai Aug 10 '22 at 11:17

1 Answers1

2

Use Test.file instead of Test[file].

curl --location --request POST 'https://localhost:7212/weatherforecast' \
--form 'id="1212"' \
--form 'name="qweqweqw"' \
--form 'image=@"/<Folder-path>/sun-flower-transparent-background-additional-png-file-greeting-cards-holiday-wishes-sun-flower-transparent-129371261.jpg"' \
--form 'Test[desc]="csdds"' \
--form 'Test.file=@"/<Folder-path>/Fruit-PNG.png"'

Demo

enter image description here

Yong Shun
  • 35,286
  • 4
  • 24
  • 46