GettingStarted
GettingStarted

Reputation: 7605

C# Data Annotations not showing correct Display Name

Here is a snippet from my Model class

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Birth Day")]
    public DateTime customerBirthDate { get; set; } = DateTime.Today;

Here is a snippet from my razor file

                <div class="wrap-input100 validate-input">
                    <span class="label-input100">Date of Birth</span>
                    <InputDate class="input100" id="birthday" name="birthday" @bind-Value="CurrentCustomerSubmission.customerBirthDate" />
                    <ValidationMessage For="@(() => Model.customerBirthDate)" />
                </div>

When i delete the date from the InputDate field, I am expecting it to say

"The Birth Day field must be a date."

but what I am actually seeing is

"The customerBirthDate field must be a date."

I tested it in Chrome

Upvotes: 1

Views: 824

Answers (2)

shayan kamalzadeh
shayan kamalzadeh

Reputation: 124

    [Required(ErrorMessage ="{0} field  is Required"]
    [DataType(DataType.Date,ErrorMessage = "{0} field must be a date.")]
    [Display(Name = "Birth Day")]
    public DateTime customerBirthDate { get; set; } = DateTime.Today;

You can use error message for the required attribute

Upvotes: 0

user14203354
user14203354

Reputation:

You can do it like this

[Required]
[DataType(DataType.Date,ErrorMessage = "The Birth Day field must be a date.")]
[Display(Name = "Birth Day")]
public DateTime customerBirthDate { get; set; } = DateTime.Today;

Upvotes: 1

Related Questions