开发者

Setting initial value for DataType.DateTime in model

开发者 https://www.devze.com 2023-02-06 07:25 出处:网络
I\'m passing a model to a view, and the model contains this attribute: [Required(ErrorMessage = \"Please enter a start date\")]

I'm passing a model to a view, and the model contains this attribute:

[Required(ErrorMessage = "Please enter a start date")]
[DataTy开发者_开发技巧pe(DataType.DateTime)]
[DisplayName("Start Date")]
public DateTime StartDate { get; set; }

And I have this in my view:

<%: Html.TextBoxFor(m => m.StartDate) %>

When I load the page, the textbox is populated with a date: 1/1/0001 12:00:00 AM

Is there a way to not have this happen, maybe by using metadata?


In the constructor, programmatically set the StartDate property to the date you want to use as the default. (Such as DateTime.Today.)

If you want the value to be initially empty, try using a Nullable<DateTime> which will default to null.


Use DateTime? ASP.net MVC Data Annotations DateTime Default Value

0

精彩评论

暂无评论...
验证码 换一张
取 消