使用自定义DataAnnotation属性比较两个属性?

Keh*_*mme 1 validation model properties data-annotations asp.net-mvc-4

我有一个包含一些DateTime值的自定义模型,以及一个DataAnnotation用于比较这些值的自定义模型.

这是带有注释的属性:

[Required]
[DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
[Display(Name = "Start Date")]
public DateTime StartTime { get; set; }

[DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
[Display(Name = "End Date")]
[CompareTo(this.StartTime, CompareToAttribute.CompareOperator.GreaterThanEqual)]
public DateTime? EndTime { get; set; }
Run Code Online (Sandbox Code Playgroud)

CompareTo属性是有问题的属性.我收到一个错误:

Keyword 'this' is not available in the current context
Run Code Online (Sandbox Code Playgroud)

我试过只StartTime放在注释中没有运气.如何从同一个模型类传递属性值?

Has*_*Ch. 5

如果有人仍然想知道如何比较两个日期并在验证DataAnnotation中使用它,您只需添加一个扩展方法,将比较开始日期和结束日期,如下所示.

假设这是你的班级:

using System;
using System.ComponentModel.DataAnnotations;

namespace Entities.Models
{
    public class Periode
    {
        [Key]
        public int PeriodeID { get; set; }

        public string Name { get; set; }

        [DataType(DataType.Date)]
        [Display(Name ="Start Date")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime StartDate { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "End Date")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EndDate { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

您只需将以下类添加为验证器:

namespace Entities.Models
{

    public class StartEndDateValidator : ValidationAttribute
    {
        protected override ValidationResult
                IsValid(object value, ValidationContext validationContext)
        {
            var model = (Models.Periode)validationContext.ObjectInstance;
            DateTime EndDate = Convert.ToDateTime(model.EndDate);
            DateTime StartDate = Convert.ToDateTime(value);

            if (StartDate > EndDate)
            {
                return new ValidationResult
                    ("The start date must be anterior to the end date");
            }
            else
            {
                return ValidationResult.Success;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要在StartDate上添加该DataAnnotation,如下所示

namespace Entities.Models
{
    public class Periode
    {
        [Key]
        public int PeriodeID { get; set; }

        public string Name { get; set; }

        [DataType(DataType.Date)]
        [Display(Name ="Start Date")]
        // You need to add the following line
        [StartEndDateValidator]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime StartDate { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "End Date")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EndDate { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)