Display a string in place of a NULL date

1.3k Views Asked by At

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.

1

There are 1 best solutions below

3
On BEST ANSWER

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())