'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; }
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |