[英]Html.EditorFor and custom format
我的模型中有一个属性是DateTime
。 如果属性包含DateTime.MinValue
我想得到一个空的<input />
(而不是包含'0001-01-01 00:00:00'的那个)。
那可能吗?
我发现为我工作的一个解决方案是添加强类型的局部视图(对于System.DateTime
)并将其放在Views/Shared/EditorTemplates
目录中。 File DateTime.cshtml
看起来非常像这样:
@model System.DateTime
@Html.TextBox("", Model == DateTime.MinValue ? "" : Model.ToShortDateString())
但是,这将格式化所有DateTime字段。
更多信息可以发现这个文章。
编写扩展帮助方法:
public static class DateTimeHelper
{
public static string MyEditor(this HtmlHelper helper, DateTime date)
{
if (date.Equals(DateTime.MinValue))
{
// return your an empty input: helper.TextBox ...
}
// return helper.EditorFor your datetime
}
}
然后,从你看来:
<%= Html.MyEditor(Model.YourDateTimeField) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.