使用ValidationResult验证DateTime

时间:2012-12-18 14:48:27

标签: c# silverlight validation xaml

我正在尝试验证我的DateTime对象,尽管我似乎无法使用ValidationResult。我能够为int和Strings做到这一点,但是为什么它对DateTime不起作用?

    [Required]
    [CustomValidation(typeof(DateValidator), "CrossValidateFirstDateWithSecondDate")]
    public DateTime ActiveOn { get; set; }

    [Required]
    [CustomValidation(typeof(DateValidator), "CrossValidateSecondDateWithFirstDate")]
    public DateTime? ExpiresOn { get; set; }

编辑:它没有触及我的DateValidator中的断点,所以没有任何反应:(

DateValidator

        using System.ComponentModel.DataAnnotations;
        using System;

        namespace Fsc.Feenics.Clients.Silverlight2.Web.Models.Shared
        {
            public class DateValidator
            {
                public static ValidationResult CrossValidateFirstDateWithSecondDate(DateTime date, ValidationContext context)
                {
                    var cardAssignmentInfo = context.ObjectInstance as CardAssignmentInfo;
                    if (cardAssignmentInfo.ExpiresOn.HasValue == false) return ValidationResult.Success;
                    if (cardAssignmentInfo == null) return ValidationResult.Success;
                    return ValidateFirstLessThanLast(date, cardAssignmentInfo.ExpiresOn.Value);
                }

                public static ValidationResult CrossValidateSecondDateWithFirstDate(DateTime? date, ValidationContext context)
                {
                    var cardAssignmentInfo = context.ObjectInstance as CardAssignmentInfo;
                    if (date.HasValue == false) return ValidationResult.Success;
                    if (cardAssignmentInfo == null) return ValidationResult.Success;
                    return ValidateFirstLessThanLast(cardAssignmentInfo.ActiveOn, date.Value);
                }

                private static ValidationResult ValidateFirstLessThanLast(DateTime firstDate, DateTime? secondDate)
                {
                    return firstDate <= secondDate.Value ? ValidationResult.Success : new ValidationResult("First number in range must be less than or equal to last number in range", new string[] { "ActiveOn", "ExpiresOn" });
                }
            }
        }

编辑:我的老板说我们使用“InvokeOperation”和RIA Services将一个大对象的数据发送到服务器然后发送到我们的数据库。

编辑:即使我让它不可为空,它也没有达到断点

0 个答案:

没有答案