在模型中设定初始值DataType.DateTime [英] Setting initial value for DataType.DateTime in model

查看:807
本文介绍了在模型中设定初始值DataType.DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个模型视图,以及模型中包含此属性:

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

[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

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?

推荐答案

在构造函数中,编程设置起始日期属性你想要作为默认使用的日期。 (如DateTime.Today。)

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

如果您想要的值是最初为空,尝试使用可空&LT; D​​ateTime的方式&gt; 这将默认为null。

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

这篇关于在模型中设定初始值DataType.DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆