DisplayTemplate not working for nullable DateTime

1k views Asked by At

Below is the the display template I created for a nullable DateTime property. I have this stored in the Views\Shared\EditorTemplates folder. The name of this template is DisplayNullableDate.cshtml.

@model DateTime?

@Html.Label("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "form-control" })

I am calling this template like so on my Details page, however it's not working. I'm still getting the time when it's displayed on the web page.

<tr>
    <td>@Html.DisplayFor(model => model.RelationshipCode1.EffectiveDate, "DisplayNullableDate", new { @class = "relCodeDate1" })</td>
    <td>@Html.DisplayFor(model => model.RelationshipCode1.RelationshipId, new { @class = "relDistCode1", maxlength = 3 })</td>
</tr>
<tr>
    <td>@Html.DisplayFor(model => model.RelationshipCode2.EffectiveDate, "DisplayNullableDate", new { @class = "relCodeDate2" })</td>
    <td>@Html.DisplayFor(model => model.RelationshipCode2.RelationshipId, new { @class = "relDistCode2", maxlength = 3 })</td>
</tr>
<tr>
    <td>@Html.DisplayFor(model => model.RelationshipCode3.EffectiveDate, "DisplayNullableDate", new { @class = "relCodeDate3" })</td>
    <td>@Html.DisplayFor(model => model.RelationshipCode3.RelationshipId, new { @class = "relDistCode3", maxlength = 3 })</td>
</tr>
<tr>
    <td>@Html.DisplayFor(model => model.RelationshipCode4.EffectiveDate, "DisplayNullableDate", new { @class = "relCodeDate4" })</td>
    <td>@Html.DisplayFor(model => model.RelationshipCode4.RelationshipId, new { @class = "relDistCode4", maxlength = 3 })</td>
</tr>
<tr>
    <td>@Html.DisplayFor(model => model.RelationshipCode5.EffectiveDate, "DisplayNullableDate", new { @class = "relCodeDate5" })</td>
    <td>@Html.DisplayFor(model => model.RelationshipCode5.RelationshipId, new { @class = "relDistCode5", maxlength = 3 })</td>
</tr>

I do have another template for the same nullable property in the same Shared\EditorTemplates folder by the name of NullableDate.cshtml. I don't think this would interfere, however I'll post it anyway just in case.

@model DateTime?

@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "form-control" })

EDIT

For good measure, here is the model property as it stands right now. Unfortunately I can't get this display format decoration to work either.

    [DisplayName("Effective Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public Nullable<System.DateTime> EffectiveDate { get; set; }
2

There are 2 answers

1
Matthew Dresser On

I think you need to decorate your model's properties with the [UIHint("NullableDate")] attribute.

0
glenn garson On

This DisplayTemplate ended up working for me, when I had null values in the DateTime field:

@model DateTime?

@((@Model != null) ?  Model.Value.ToString("MM/dd/yyyy") : "{n/a}" )

When the value is null, it is replaced with the string: {n/a}