日期控制比较验证

时间:2013-05-22 07:22:41

标签: asp.net-mvc-3 asp.net-mvc-4

我正在寻找一种方法来执行数据比较验证,验证“来自”> “To”在数据模型客户端和服务器上。 像这样:

public sealed class CompareDatesValidatorAttribute:ValidationAttribute     {         private string _dateToCompareField;

    public string FieldToCompare
    {
        get { return _dateToCompareField; }
        set { _dateToCompareField=value; }
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(this.ErrorMessageString, name, FieldToCompare);

    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var dateToCompare = validationContext.ObjectType.GetProperty(_dateToCompareField);
        var dateToCompareValue = dateToCompare.GetValue(validationContext.ObjectInstance, null);
        if (dateToCompareValue != null && value != null && (DateTime)value < (DateTime)dateToCompareValue)
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
        return null;
    }

我已经开发了服务器验证,但我正在为此构建解决方案。适用于客户端和服务器。

任何帮助将不胜感激,

10倍 罗尼

0 个答案:

没有答案
相关问题