是否有日期时间一RangeAttribute? [英] Is there a RangeAttribute for DateTime?
本文介绍了是否有日期时间一RangeAttribute?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个日期时间字段在我的模型,需要验证它使创建时它具有的回落立即和 6年之前,。我一直在使用范围如
I have a Datetime field in my Model and need to validate it so that when it is created it has to fall between Now and 6 Years Prior. I have tried using range like
[Range(DateTime.Now.AddYears(-6), DateTime.Now)]
public DateTime Datetim { get; set; }
但是,这将引发一个错误不能转换系统日期时间翻番。任何人都可以提出一个解决方法,这模型本身?
But this throws an error cannot convert system datetime to double. Can anyone suggest a workaround to this in the model itself?
推荐答案
使用这个属性:
public class CustomDateAttribute : RangeAttribute
{
public CustomDateAttribute()
: base(typeof(DateTime),
DateTime.Now.AddYears(-6).ToShortDateString(),
DateTime.Now.ToShortDateString())
{ }
}
这篇关于是否有日期时间一RangeAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文