How do I Default the date to todays date for required field using EditForm?

Viewed 2770

I have a required property(using DataAnnotations) that defaults all the values without a ? mark after the type. it defaults to 01/01/0001. How can I make this blank or default to a certain date?

P.S I also had two int properties and they defaulted to 0. This makes no sense since I have no idea how it defaults.

Model property: enter image description here

Form field: enter image description here

enter image description here

2 Answers

Just add a default value in the model

[Required(ErrorMessage = "The Job Start Date field is Required")]
public DateTime StartDate { get; set; } = DateTime.Now;

Asides adding a default value you can override the OnInitialized blazor component method and set a default value in the component.

protected override void OnInitialized()
{
   request.StartDate = DateTime.Now;
}
Related