我在我的模型中获得了一个属性,它是DateTime
。我想获得一个空的<input />
(而不是一个包含'0001-01-01 00:00:00')如果该房产包含DateTime.MinValue
。Html.EditorFor和自定义格式
这可能吗?
我在我的模型中获得了一个属性,它是DateTime
。我想获得一个空的<input />
(而不是一个包含'0001-01-01 00:00:00')如果该房产包含DateTime.MinValue
。Html.EditorFor和自定义格式
这可能吗?
一个解决方案,我发现工作对我来说是添加强类型的局部视图(System.DateTime
)和把它放在Views/Shared/EditorTemplates
目录下。文件DateTime.cshtml
看起来非常像这样:但是
@model System.DateTime
@Html.TextBox("", Model == DateTime.MinValue ? "" : Model.ToShortDateString())
这将格式化所有的日期时间字段。
更多信息可在this文章中找到。
写一个extension helper方法:
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) %>