Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Default value as null for InputDate field in Blazor?

I have many InputDate fields in my editform of Blazor. When I run my code, by default its set to 01/01/0001. It kinda looks weird to me . I feel it should be set to Blank by default. So that when user will miss out entering the date error will pop up(as OrderDate is set to Required). Is there any Blazor way or doing it?

Some code snippet:

                <div class="form-group col">
                    <label>Order Date</label>
                    <InputDate @bind-Value="model.OrderDate" class="form-control" />
                    <ValidationMessage For="@(() => model.OrderDate)" />
                </div>

Model code:

        [Required]
        [Display(Name = "OrderDate")]
        public DateTime OrderDate { get; set; }
like image 668
Shrey Avatar asked Oct 29 '25 17:10

Shrey


1 Answers

Blazor is not to blame here, it's a .net thing. 01-01-0001 is a valid date (DateTime.MinValue and also default(DateTime) ).

The property in your model is not nullable, so what you see is correct. When you want null values you have to change your model:

//public DateTime OrderDate { get; set; }
  public DateTime? OrderDate { get; set; }

Blazor does support nullable value types.

like image 146
Henk Holterman Avatar answered Oct 31 '25 07:10

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!