'.NET web API one to many relation

I'm building a one to many relation web API with .NET and got error 400. I have two tables:

 public class CarBrand
    {
        public int CarBrandId { get; set; }
        public string Name { get; set; }
        [JsonIgnore]
        public List<CarModel> CarModels { get; set; } = new List<CarModel>();
    }

    public class CarModel
    {
        public int CarModelId { get; set; }
        public string ModelName { get; set; }
        public int CarBrandId { get; set; }
        [JsonIgnore]
        public CarBrand CarBrand { get; set; } = new CarBrand();
    }

My Controller is:

    [HttpPost]
    public async Task<ActionResult<CarModel>> PostCarModel(CarModel carModel)
    {
        _context.CarModels.Add(carModel);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetCarModel", new { id = carModel.CarModelId }, carModel);
    }

But when I make a POST request and try to add CarModel, I get error 400:

CarBrand:[
    "This Field is required!"
]

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source