我在我的模型中獲得了一個屬性,它是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) %>