Search code examples
blazormudblazor

Blazor MudTable - Null Dates in Table


Can anyone please advise how I can set the date value in a row to "" if the source data is a null date (01/01/0001)

I have tried this way with no luck..

<MudTd DataLabel="EndDate" Style="width: 160px">@row.EndDate == @null ? '' : @Convert.ToDateTime(row.EndDate).ToString("dd/MM/yyyy")</MudTd>

Regards Peter


Solution

  • For that syntax to work (using the conditional operator), you need to wrap the whole expression in script block, this is the same rule for string interpolation:

    <MudTd DataLabel="EndDate" 
           Style="width: 160px">
        @(row.EndDate == null ? "" : Convert.ToDateTime(row.EndDate).ToString("dd/MM/yyyy"))
    </MudTd>
    

    Given that you are casting to a date, is the value even null in the first place? if it is a string, try IsNullOrEmpty / IsNullOrWhiteSpace ...

    <MudTd DataLabel="EndDate" 
           Style="width: 160px">
        @(String.IsNullOrWhiteSpace(row.EndDate) ? "" : Convert.ToDateTime(row.EndDate).ToString("dd/MM/yyyy"))
    </MudTd>
    

    If the value is a nullable DateTime? then do not use Convert.ToDateTime or conditional operator (in-line if expression), just use String.Format which will implicitly convert a null value to an empty string:

    <MudTd>@String.Format("{0:dd/MM/yyyy}", @row.EndDate)</MudTd>
    

    Try this in MudBlazor here: https://try.mudblazor.com/snippet/QkcRFPmgFqxqgoRW