Tôi đang cố định dạng một số DateTimes trong MVC nhưng DisplayFormat không được áp dụng cho đối tượng Nullable và tôi không thể hiểu tại sao. Nó hoạt động hoàn toàn tốt đẹp trên CreatedDateTime nhưng không LastModifiedDateTimeDisplayFormat không hoạt động với Nullable <DateTime>
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yy hh:mm tt}")]
public DateTime CreatedDateTime { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yy hh:mm tt}")]
public Nullable<DateTime> LastModifiedDateTime { get; set; }
Dưới đây là Xem
<div class="editor-field">
@Html.DisplayFor(model => model.CreatedDateTime)
<br />
@Html.Raw(TimeAgo.getStringTime(Model.CreatedDateTime))
</div>
@if (Model.LastModifiedDateTime.HasValue)
{
<div class="editor-label">
@Html.LabelFor(model => model.LastModifiedDateTime)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.LastModifiedDateTime)
<br />
@Html.Raw(TimeAgo.getStringTime(Model.LastModifiedDateTime.Value)) By: @Html.DisplayFor(model => model.LastModifiedBy)
</div>
}
Không thành công nếu bạn sử dụng 'DateTime?' Thay vì 'Nullable'? –
TyCobb
Thay đổi nó thành DateTime? không có hiệu lực. – Preston
@TyCobb 'DateTime?' Và 'Nullable' giống hệt nhau. –