日期的范围属性
Range Attribute for Dates
我有这个 class 覆盖 RangeAttribute:
public class RangeDateAttribute : RangeAttribute
{
public RangeDateAttribute()
: base(typeof(DateTime),
DateTime.Now.AddYears(-20).ToShortDateString(), DateTime.Today.ToShortDateString()) { }
}
我的属性的数据注释:
[RangeDate(ErrorMessage = "Value for {0} must be between {1} and {2}")]
public DateTime anyDate { get; set; }
我在该验证中使用方法 [ToShortDateString()],但是当显示错误时,它随时间而来。例如:
Value for anyDate must be between 26/05/1995 00:00:00 and 26/05/2015 00:00:00
我该如何解决这个问题?
谢谢。
您必须使用例如 ArgumentException(String) 方法格式化错误消息,并应用与短日期相关的格式规范,例如 String.Format("{0:d}", dt)。希望这可能有所帮助。最好的问候,
您可以对错误消息属性使用一些格式:
[RangeDate(ErrorMessage = "Value for {0} must be between {1:dd/MM/yyyy} and {2:dd/MM/yyyy}")]
public DateTime anyDate { get; set; }
我有这个 class 覆盖 RangeAttribute:
public class RangeDateAttribute : RangeAttribute
{
public RangeDateAttribute()
: base(typeof(DateTime),
DateTime.Now.AddYears(-20).ToShortDateString(), DateTime.Today.ToShortDateString()) { }
}
我的属性的数据注释:
[RangeDate(ErrorMessage = "Value for {0} must be between {1} and {2}")]
public DateTime anyDate { get; set; }
我在该验证中使用方法 [ToShortDateString()],但是当显示错误时,它随时间而来。例如:
Value for anyDate must be between 26/05/1995 00:00:00 and 26/05/2015 00:00:00
我该如何解决这个问题?
谢谢。
您必须使用例如 ArgumentException(String) 方法格式化错误消息,并应用与短日期相关的格式规范,例如 String.Format("{0:d}", dt)。希望这可能有所帮助。最好的问候,
您可以对错误消息属性使用一些格式:
[RangeDate(ErrorMessage = "Value for {0} must be between {1:dd/MM/yyyy} and {2:dd/MM/yyyy}")]
public DateTime anyDate { get; set; }