I just moved to ASP.NET Core 3.0 and it seems that either model validation with inheritance is broken, or I am missing something. When I post the following model with IlVal00 = null
, it says:
title=One or more validation errors occurred. status=400, The IlVal00 field is required.
public class Stock : BaseClass
{
[Required]
public string Ref { get; set; } = default!;
}
public class BaseClass
{
public string? IlVal00 { get; set; }
}
But if I change my model to the following, it works:
public class Stock : BaseClass
{
[Required]
public string Ref { get; set; } = default!;
public new string? IlVal00 { get; set; }
}
public class BaseClass
{
public string? IlVal00 { get; set; }
}
But I need this to support inheritance. Does anyone have an idea for how to make this work?
Thanks.
services.Configure<ApiBehaviorOptions>(options => { options.SuppressModelStateInvalidFilter = true; });
– ZugzwangMvcOptions.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true
. Then you can specify[Required]
attribute explicitly. – Reface