在模型中设置DataType.DateTime的初始值

Ste*_*ven 1 c# asp.net asp.net-mvc

我将模型传递给视图,模型包含此属性:

[Required(ErrorMessage = "Please enter a start date")]
[DataType(DataType.DateTime)]
[DisplayName("Start Date")]
public DateTime StartDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我看来,我有这个:

<%: Html.TextBoxFor(m => m.StartDate) %>
Run Code Online (Sandbox Code Playgroud)

当我加载页面时,文本框中填充了一个日期: 1/1/0001 12:00:00 AM

有没有办法让这种情况发生,也许是通过使用元数据?

Jos*_*osh 5

在构造函数中,以编程方式将StartDate属性设置为要用作默认值的日期.(例如DateTime.Today.)

如果您希望该值最初为空,请尝试使用Nullable<DateTime>默认为null的值.