Referencing the answer in this post I added /Views/Shared/DisplayTemplates and added a partial view called ShortDateTime.cshtml as shown below:
@model System
The problem you're experiencing is that you are passing a null
value into a non-nullable model. Change the partial view's model to DateTime?
. For example:
@model DateTime?
@if (!Model.HasValue)
{
<text></text>
}
else
{
@Model.Value.ToShortDateString()
}
Hope this helps.
I guess you should declare the BirthDate property as nullable in model class
public DateTime? BirthDate{ get; set; }
and you must have declared as
public DateTime BirthDate{ get; set; }
this will expect a value every time.
if you set as nullable it will not expect a value.
Have you tried this?
@if (modelItem.BirthDate != null) { Html.DisplayFor(modelItem => item.BirthDate, "ShortDateTime") }
By applying the conditional statement ahead of the Html helper, only non null values get passed.
@if (item.BirthDate != null) { @Html.DisplayFor(modelItem => item.BirthDate , "ShortDateTime")}