我正在将模型传递给 View ,模型包含此属性:
[Required(ErrorMessage = "Please enter a start date")]
[DataType(DataType.DateTime)]
[DisplayName("Start Date")]
public DateTime StartDate { get; set; }
我的观点是:
<%: Html.TextBoxFor(m => m.StartDate) %>
当我加载页面时,文本框会填充一个日期:1/1/0001 12:00:00 AM
有没有办法避免这种情况发生,也许是通过使用元数据?
最佳答案
在构造函数中,以编程方式将 StartDate 属性设置为要用作默认日期的日期。 (例如 DateTime.Today。)
如果您希望该值最初为空,请尝试使用 Nullable<DateTime>
这将默认为 null。
关于c# - 在模型中设置 DataType.DateTime 的初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4790096/