1

Here are my models

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<CategoryDetail> CategoryDetails { get; set; }
}

public class CategoryDetail
{
    public int Id { get; set; }
    public string Url { get; set; }
    public IFormFile File { get; set; }
    public Category Category { get; set; }
}

and my controller function is

    [HttpPost]
    public IActionResult Create([FromForm] Category category)
    {
        throw new NotImplementedException();
    }

the parameter inside the controller method is always null when I pass data through postman.

enter image description here

Uttam Kar
  • 173
  • 1
  • 10
  • You need to parse the multipart/formdata request, take alook at this https://stackoverflow.com/questions/34979051/find-next-instance-of-a-given-weekday-ie-monday-with-moment-js – tbc Nov 15 '21 at 18:06

1 Answers1

1

You cannot pass two different content type data to backend.

Your key name is wrong, it should be CategoryDetails[0].File not CategoryDetails[0].files. The key name should match the property name and it is case insensitive.

The correct way should be like below:

enter image description here

Note:

If you post CategoryDetails with only File property, you need be sure remove [ApiController] and [FromForm] attribute. It is a known github issue here. If you post CategoryDetails with several properties, no need to remove any attribute.

Rena
  • 30,832
  • 6
  • 37
  • 72
  • Thanks for your cooperation Rena. Its working now. – Uttam Kar Nov 16 '21 at 05:32
  • I also set different content type for different key and it worked correctly – Uttam Kar Nov 16 '21 at 05:45
  • Hi @UttamKar, where did you set? Do you mean you set `CONTENT TYPE` beside the `Value`. Maybe I do not explain to you clearly. `CONTENT TYPE` in the form-data panel is not used to set the content type. It just something like comment. `cannot pass two different content type data` means you set the `category` key with `json` string, and set `CategoryDetails[0].File` key with `multipart/form-data` type data. Model binding cannot auto bind the model with two different content type data. – Rena Nov 16 '21 at 05:58
  • thank you @Rena for your explanation. I got it what does "cannot pass two different content type data" mean. – Uttam Kar Nov 16 '21 at 06:06