Home > Blockchain >  I have two models in .net5 api application with one to many relationship. child model contains IForm
I have two models in .net5 api application with one to many relationship. child model contains IForm

Time:11-16

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

CodePudding user response:

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.

  • Related