0

我正在尝试验证我的 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 中达到我的断点,所以什么也没有发生:(

日期验证器

        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 服务将一个大对象中的数据发送到服务器,然后发送到我们的数据库。

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

4

0 回答 0