Search code examples
c#htmlblazorblazor-client-sideasp.net-blazor

How to add placeholder or 0 to Blazor Date


<InputDate id="birthday" class="form-control"   placeholder=@Loc["birthday"] @bind-Value="PersModel.Pers.GeburtsDatum" />

public DateTime GeburtsDatum { get; set; }

I want the Default value to be any placeholder or Nothing only when date selected to be changed

i want the Default value to be any placeholder or Nothing only when date selected to be changed

 pers.GeburtsDatumString = pers.GeburtsDatum.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

whenever i Change the

public DateTime? GeburtsDatum { get; set; }  to be able to be 0 i get error at 


pers.GeburtsDatumString = pers.GeburtsDatum.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

error here


Solution

  • You can use the null-conditional operator to handle nullable DateTime:

    pers.GeburtsDatumString = pers.GeburtsDatum?.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
    //                                         ^   
    

    This returns null when pers.GeburtsDatum.HasValue is false (ie, when the value is null).