MVC.Net Core Model Validation - '' value is invalid. mistake

I am trying to use model validation in MVC.Net Core and cannot replace this default error message "Value" is not valid.

In theory, we can replace our own error message using ErrorMessageAnnotation in the model. But I could not find a way to make this 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 ErrorMessagefor tags Requiredand DataTypeas shown 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 help me, how can I replace this error message? Thanks.

+13
source share
3 answers

Required, :

public DateTime? AppointmentDate { get; set; }

: , DataType . MVC post model

+13

, Create Action View Entity, , , .. Return View(new MyEntity());,

0

Having the same problem but cannot detect the problem. I checked the object in debug mode to see if there is a way to find out which property does not match the state of the model.

Debug mode view of the modelstate object

Then I see which model does not fit. This is the boolean value that is displayed on the checkbox

Strange part: "this is not a required field"!

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

public bool? IsCorporateAccount { get; set; }
0
source

Source: https://habr.com/ru/post/1016395/


All Articles