我想为DateTime属性创建一个displayTemplate.
例如,我有以下模型:
public class MyModel { [DataType(DataType.Date)] public DateTime? Date { get; set; } [DataType(DataType.Time)] public DateTime? TimeFrom { get; set; } [DataType(DataType.Time)] public DateTime? TimeUntil { get; set; } [DataType(DataType.DateTime)] public DateTime? SomeDate { get; set; } }
在我看来:
@Html.displayFor(x => x.Date) // Expected output: 13.11.2015 @Html.displayFor(x => x.TimeFrom) // Expected output: 08:00 @Html.displayFor(x => x.TimeUntil) // Expected output: 12:00 @Html.displayFor(x => x.someDate) // Expected output: 15.11.2015 08:55
@using System.ComponentModel.DataAnnotations @using System.Reflection @model DateTime? @{ var type = Model.GetType(); var attribute = type.GetCustomAttribute(typeof (DataTypeAttribute)) as DataTypeAttribute; } @if (Model != null) { if (attribute != null) { if (attribute.DataType == DataType.Date) { @Model.Value.ToShortDateString() } if (attribute.DataType == DataType.Time) { @Model.Value.ToShortTimeString() } else { @Model.Value } } else { @Model.Value } }
不幸的是,找不到DataTypeAttribute.
找到的唯一CustomAttributes是Serializable和__DynamicallyInvokable-Attributes.
在模型中,我们总是有DateTimes的DataType属性.
如何找到当前的DataTypeAttribute?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。