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>
Could you please help me how I could get that error message replaced? Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…