编程语言
首页 > 编程语言> > c#-开始日期和结束日期的数据注释

c#-开始日期和结束日期的数据注释

作者:互联网

我正在使用视图模型作为开始日期和结束日期,并使用通过数据注释的验证.验证规则为:

>开始日期不应大于结束日期,并且不能为null,空白
>结束日期不得少于开始日期且不得少于
DateTime.Now,不为null,空白

跟随的View Model由startdate和enddate属性组成.混乱之处在于如何在以下代码中传递StartEndDateRange数据注释中的EndDate值和EndDate数据注释中的StartDate值:

  public class StartEndDate
    {
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [StartEndDateRange("2000/01/01", "value of end date properties")]
        public DateTime StartDate { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [StartEndDateRange("value of startdate properties", DateTime.Now.ToString("yyyy/MM/dd"))]
        public DateTime EndDate { get; set; }


    }

    public class StartEndDateRangeAttribute : ValidationAttribute
    {
        private const string DateFormat = "yyyy/MM/dd";
        private const string DefaultErrorMessage =
     "'{0}' must be a date between {1:d} and {2:d}.";

        public DateTime MinDate { get; set; }
        public DateTime MaxDate { get; set; }

        public StartEndDateRangeAttribute(string minDate, string maxDate)
            : base(DefaultErrorMessage)
        {
            MinDate = ParseDate(minDate);
            MaxDate = ParseDate(maxDate);
        }

        public override bool IsValid(object value)
        {
            if (value == null || !(value is DateTime))
            {
                return true;
            }
            DateTime dateValue = (DateTime)value;
            return MinDate <= dateValue && dateValue <= MaxDate;
        }
        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture,
     ErrorMessageString,
                name, MinDate, MaxDate);
        }

        private static DateTime ParseDate(string dateValue)
        {
            return DateTime.ParseExact(dateValue, DateFormat,
     CultureInfo.InvariantCulture);
        }
    }

解决方法:

创建两个不同的验证属性.一个日期之前和另一个数据之后

您可以从validationContext获取验证下的模型属性.

在DateBefore之前

protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
    PropertyInfo endDateProperty= validationContext.ObjectType.GetProperty("EndDate");
    ...
}

通过获取价值

var endDate = endDateProperty.GetValue(validationContext.ObjectInstance, null);

现在比较值和endDate.

编辑

多一点代码

public class BeforeEndDateAttribute : ValidationAttribute{
    public string EndDatePropertyName { get; set; }
    public string StartDate { get; set; }


    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        PropertyInfo endDateProperty = validationContext.ObjectType.GetProperty(EndDatePropertyName);

        DateTime endDate = (DateTime) endDateProperty.GetValue(validationContext.ObjectInstance, null);

        var startDate = DateTime.Parse(StartDate);

        // Do comparison
        // return ValidationResult.Success; // if success
        return new ValidationResult("Error"); // if fail
    }

}

并使用像:

public class MyModel
{
    [BeforeEndDate(EndDatePropertyName = "EndDate", StartDate = "2000/01/01")]
    public DateTime StartDate { get; set; }

    // [AfterStartDate(StartDatePropertyName = "StartDate", EndDate = "2020/01/01")]
    public DateTime EndDate { get; set; }
}

标签:data-annotations,c,asp-net-mvc-3
来源: https://codeday.me/bug/20191031/1979712.html