DisplayTemplate无法用于可为null的DateTime [英] DisplayTemplate not working for nullable DateTime

查看:93
本文介绍了DisplayTemplate无法用于可为null的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我为可为空的DateTime属性创建的显示模板.我将其存储在Views\Shared\EditorTemplates文件夹中.该模板的名称为DisplayNullableDate.cshtml.

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" })

我在Details页面上像这样称呼此模板,但是它不起作用.我仍然有时间在网页上显示它.

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>

在相同的Shared\EditorTemplates文件夹中,我确实有另一个模板,其名称为NullableDate.cshtml,用于同一可为空的属性.我认为这不会造成干扰,但是无论如何我都会将其发布,以防万一.

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" })

编辑

为了很好地衡量,这里是当前状态的模型属性.不幸的是,我也无法使这种显示格式的装饰正常工作.

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

推荐答案

当我在DateTime字段中具有空值时,此DisplayTemplate最终为我工作:

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}" )

当该值为null时,将其替换为字符串:{n/a}

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

这篇关于DisplayTemplate无法用于可为null的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆