• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

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

I am trying to use Model Validation in MVC .Net Core and can't manage to replace this default error message 'The value '' is invalid'.

In theory, we can replace our own custom error message by using ErrorMessage Annotation in the Model. But I couldn't find a way to make this one work.

My Model

[Required(ErrorMessage = "Date Required")]
[DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]                
[Display(Name = "Appointment Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime AppointmentDate { get; set; }

I put different ErrorMessage for both Required and DataType tag as shown in the above.

My html view

    <div class="col-md-2">
        <input class="form-control" asp-for="AppointmentDate">
        <span asp-validation-for="AppointmentDate" class="text-danger"></span>
    </div>

enter image description here

Could you please help me how I could get that error message replaced? Thanks.

almost 3 years ago · Santiago Trujillo
3 answers
Answer question

0

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

almost 3 years ago · Santiago Trujillo Report

0

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.

almost 3 years ago · Santiago Trujillo Report

0

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; }
almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error