Annotation [Required] for the DateTime in class model

sblb 1,166 Reputation points
2022-09-29T14:18:19.927+00:00

Hi,

I want to put the data annotation for the DateTime but it is not taken into account

[Required]  
public DateTime DateCrea { get; set; }  

i.e. in my application if I don't enter a value I can validate my page (e.g. creation).
Do you have any idea where the problem comes from? Should I add something specific for the DateTime?

Thanks in advance

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,382 questions
{count} votes

2 answers

Sort by: Most helpful
  1. sblb 1,166 Reputation points
    2022-09-29T15:42:15.46+00:00

    Are you asking about an Entity Framework class model

    yes

    A View model in your Blazor application?

    yes

    Model binding in Web API?

    no

    Is DateCrea a column in a table ... are you expecting a required user message in a Blazor application?

    yes, I expect a message, so the user must fill in the DateCrea field.


  2. Bruce (SqlWork.com) 55,366 Reputation points
    2022-09-29T15:57:35.753+00:00

    show your form. as a date time is not nullable, its required by default.