MVC .Net Core Model Validation - The value '' is invalid. Error

In order to make your Required attribute works you need to make field nullable:

public DateTime? AppointmentDate { get; set; }

Edit: also note that DataType attribute actually doesn't perform validation on field. MVC validate date when applying binding from post data to model


Having the same problem but cannot detect the problem. I checked the object in debug mode to see if is there any way to see which property fails the model state.

Debug mode view of the modelstate object

Then I see the which one fails model. That is a boolean value which maps to a checkbox

Weird part is "this is not a Required field"!

I added a question mark and used getvalueordefault method when using it

public bool? IsCorporateAccount { get; set; }

After .NET Core 3 validation system changed. Non-nullable parameters are treated as if they had a [Required] attribute. You get client side validation even if you don't apply the [Required] attribute. Client side JQuery validation accepts empty strings fields but once sent to server the same field will get the invalid result. The value '' is invalid is the default error message for server side validation. According to asp.net docs by using a [Required] attribute you can override this message but it does not apply to empty fields. Unfortunately this feature generates empty string values ("") for hidden input fields that reference non-nullable int properties (i.e @Html.Hiddenfor(m=>m.id) would generate "" for the html element.) So out of all the options provided in asp.net docs the safest one is making the property nullable. another good option is changing .NET default message to something else

services.AddRazorPages()
    .AddMvcOptions(options =>
    {
        options.ModelBindingMessageProvider.SetValueMustNotBeNullAccessor(
            _ => "The field is required.");
    });

You can read more about this here.


In some case the validation summary can be the cause: Change the "All" for "ModelOnly":

<div asp-validation-summary="All" class="text-danger"></div>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>