Search code examples
asp.net-mvcrazorrazor-declarative-helpers

Display a string in place of a NULL date


when the date property is set in the model, this is executed:

if (!(rdr["JobEnded"] is DBNull)) { job.JobEnded = (DateTime)rdr["JobEnded"]; }

which results in job.JobEnded being 1/1/0001 12:00:00 AM.

What can I use in place of

@Html.DisplayFor(modelItem => item.JobEnded)

to show "In Progress" instead of "1/1/0001 12:00:00 AM".

I've tried putting an if/else in the view, but was only able to display a valid date or nothing. I'm sure I'm missing something really basic here as this is my first attempt at an APS.NET MVC view.


Solution

  • You're dealing with default dates here, so to test for it, you need to use:

    @if (item.JobEnded == default(DateTime))
    {
        @:In Progress
    }
    else
    {
        @Html.DisplayFor(m => item.JobEnded)
    }
    

    Or in one-liner form:

    @((item.JobEnded == default(DateTime)) ? "In Progress" : Html.DisplayFor(m => item.JobEnded).ToString())