C# DisplayTemplate不适用于可为空的日期时间
下面是我为可为空的C# DisplayTemplate不适用于可为空的日期时间,c#,.net,asp.net-mvc-3,razor,mvc-editor-templates,C#,.net,Asp.net Mvc 3,Razor,Mvc Editor Templates,下面是我为可为空的DateTime属性创建的显示模板。我将其存储在Views\Shared\EditorTemplates文件夹中。此模板的名称为DisplayNullableDate.cshtml @model DateTime? @Html.Label("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "form-control" }) 我在我的详细信息页面上这
DateTime
属性创建的显示模板。我将其存储在Views\Shared\EditorTemplates
文件夹中。此模板的名称为DisplayNullableDate.cshtml
@model DateTime?
@Html.Label("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "form-control" })
我在我的详细信息
页面上这样调用此模板,但它不起作用。我仍然能得到它在网页上显示的时间
<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>
编辑
为了更好地衡量,这里是当前的模型属性。不幸的是,我也不能让这个显示格式装饰工作
[DisplayName("Effective Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> EffectiveDate { get; set; }
[DisplayName(“生效日期”)]
[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:MM/dd/yyyy}”)]
公共可空的EffectiveDate{get;set;}
我认为您需要使用[UIHint(“NullableDate”)]
属性来装饰模型的属性。当我在DateTime字段中有空值时,这个显示模板就为我工作了:
@model DateTime?
@((@Model != null) ? Model.Value.ToString("MM/dd/yyyy") : "{n/a}" )
当值为null时,它将替换为字符串:{n/a}Hm。。。目前没有,但是
NullableDate
模板可以正常工作。刚刚发布了模型属性。
@model DateTime?
@((@Model != null) ? Model.Value.ToString("MM/dd/yyyy") : "{n/a}" )