MVC3 DateTime属性的默认值 [英] MVC3 Default value for a DateTime property
本文介绍了MVC3 DateTime属性的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个Model类上有一个DateTime属性,并希望将其默认值设置为Now。以下代码是我希望可以运行但无法编译的代码。它不喜欢System.DateTime.Now调用:
I have a DateTime property on one of my Model classes and want to set it's default vaule to Now. The below code is what I had hoped would work but won't compile. It doesn't like the System.DateTime.Now call:
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[Display(Name = "To Date")]
[Required(ErrorMessage = "To Date is required.")]
[DefaultValue(System.DateTime.Now)]
public DateTime DateTo { get; set; }
有什么想法吗?
推荐答案
在模型类中:
private DateTime _date = DateTime.Now;
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
这篇关于MVC3 DateTime属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文