user9372946
user9372946

Reputation: 33

frombody object is null in .net core 3

    [HttpPost("add-car")]
    public async Task<IActionResult> AddCarAsync([FromBody] AddCarInputDtos addCarInputDtos)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
    }

    public class AddCarInputDtos
    {
        public int? CodeParent { get; set; }
        Required(ErrorMessage = "CodeCarList parameter is mandatory.")]
        public int? CodeCarList { get; set; }}
        [EnumDataType(typeof(YesNoEnum), ErrorMessage = "YesNoEnum value doesn't exist within enum")]
        public YesNoEnum? FeatureLoad { get; set; }
    }

When I call this API method with postman in application/json format and trace the code object is null and it returns:

    {
      "$.featureLoad": [
        "The JSON value could not be converted to System.Nullable`1[Jinavat.Backend.Api.Dtos.General.YesNoEnum]. Path: $.featureLoad | LineNumber: 0 | BytePositionInLine: 268."
      ]
    }

Please help me to resolve this problem.

Upvotes: 3

Views: 869

Answers (1)

Jordan Wilcken
Jordan Wilcken

Reputation: 316

Try adding NewtonsoftJson explicitly in ConfigureServices:

    public void ConfigureServices(IServiceCollection services)
    {
        // services.AddABunchOfStuff();
        services.AddNewtonsoftJson();
        // services.AddMoreStuff();
    }

as per these docs: https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support

Upvotes: 3

Related Questions