DisplayTemplate not working for nullable DateTime

≯℡__Kan透↙ 提交于 2019-12-13 04:53:59

问题


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; }

回答1:


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




回答2:


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}



来源:https://stackoverflow.com/questions/20432750/displaytemplate-not-working-for-nullable-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!