mvc4数据注释比较两个日期

时间:2022-05-21 14:18:40

I have these two fields in my model:

我的模型中有两个领域:

[Required(ErrorMessage="The start date is required")]
[Display(Name="Start Date")]
[DisplayFormat(DataFormatString = "{0,d}")]
public DateTime startDate { get; set; }

[Required(ErrorMessage="The end date is required")]
[Display(Name="End Date")]
[DisplayFormat(DataFormatString = "{0,d}")]
public DateTime endDate{ get; set; }

I require that endDate must be greater than startDate. I tried using [Compare("startDate")] but this only works for the equal operation.

我要求endDate必须大于startDate。我尝试使用[Compare("startDate")],但这只适用于相等的操作。

What should I use for the "greater than" operation?

“大于”操作应该使用什么?

2 个解决方案

#1


53  

Take a look at Fluent Validation or MVC Foolproof Validation: those can help you a lot.

看一下Fluent验证或MVC简单验证:这对您有很大的帮助。

With Foolproof for example there is a [GreaterThan("StartDate")] annotation than you can use on your date property.

例如,有一个[GreaterThan("StartDate")]注释,比您可以在date属性上使用的注释更简单。

Or if you don't want to use other libraries, you can implement your own custom validation by implementing IValidatableObject on your model:

或者如果您不想使用其他库,您可以通过在模型上实现IValidatableObject来实现您自己的自定义验证:

public class ViewModel: IValidatableObject
{
    [Required]
    public DateTime StartDate { get; set; }
    [Required]    
    public DateTime EndDate { get; set; } 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
       if (EndDate < StartDate)
       {
           yield return 
             new ValidationResult(errorMessage: "EndDate must be greater than StartDate",
                                  memberNames: new[] { "EndDate" });
       }
    }
}

#2


10  

IValidatableObject interface provides a way for an object to be validated which implements IValidatableObject.Validate(ValidationContext validationContext) method. This method always return IEnumerable object. That's why you should create list of ValidationResult objects and errors are added to this and return. Empty list means to validate your conditions. That is as follows in mvc 4...

IValidatableObject接口为实现IValidatableObject的对象提供了一种验证方法。Validate(ValidationContext ValidationContext)方法。这个方法总是返回IEnumerable。这就是为什么您应该创建ValidationResult对象的列表,并在其中添加错误并返回。空列表意味着验证您的条件。这在mvc 4中是这样的……

 public class LibProject : IValidatableObject
{
    [Required(ErrorMessage="Project name required")]

    public string Project_name { get; set; }
    [Required(ErrorMessage = "Job no required")]
    public string Job_no { get; set; }
    public string Client { get; set; }
    [DataType(DataType.Date,ErrorMessage="Invalid Date")]
    public DateTime ExpireDate { get; set; }


    IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
    {
        List < ValidationResult >  res =new List<ValidationResult>();
        if (ExpireDate < DateTime.Today)
        {
            ValidationResult mss=new ValidationResult("Expire date must be greater than or equal to Today");
            res.Add(mss);

        }
        return res; 
    }
}

#1


53  

Take a look at Fluent Validation or MVC Foolproof Validation: those can help you a lot.

看一下Fluent验证或MVC简单验证:这对您有很大的帮助。

With Foolproof for example there is a [GreaterThan("StartDate")] annotation than you can use on your date property.

例如,有一个[GreaterThan("StartDate")]注释,比您可以在date属性上使用的注释更简单。

Or if you don't want to use other libraries, you can implement your own custom validation by implementing IValidatableObject on your model:

或者如果您不想使用其他库,您可以通过在模型上实现IValidatableObject来实现您自己的自定义验证:

public class ViewModel: IValidatableObject
{
    [Required]
    public DateTime StartDate { get; set; }
    [Required]    
    public DateTime EndDate { get; set; } 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
       if (EndDate < StartDate)
       {
           yield return 
             new ValidationResult(errorMessage: "EndDate must be greater than StartDate",
                                  memberNames: new[] { "EndDate" });
       }
    }
}

#2


10  

IValidatableObject interface provides a way for an object to be validated which implements IValidatableObject.Validate(ValidationContext validationContext) method. This method always return IEnumerable object. That's why you should create list of ValidationResult objects and errors are added to this and return. Empty list means to validate your conditions. That is as follows in mvc 4...

IValidatableObject接口为实现IValidatableObject的对象提供了一种验证方法。Validate(ValidationContext ValidationContext)方法。这个方法总是返回IEnumerable。这就是为什么您应该创建ValidationResult对象的列表,并在其中添加错误并返回。空列表意味着验证您的条件。这在mvc 4中是这样的……

 public class LibProject : IValidatableObject
{
    [Required(ErrorMessage="Project name required")]

    public string Project_name { get; set; }
    [Required(ErrorMessage = "Job no required")]
    public string Job_no { get; set; }
    public string Client { get; set; }
    [DataType(DataType.Date,ErrorMessage="Invalid Date")]
    public DateTime ExpireDate { get; set; }


    IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
    {
        List < ValidationResult >  res =new List<ValidationResult>();
        if (ExpireDate < DateTime.Today)
        {
            ValidationResult mss=new ValidationResult("Expire date must be greater than or equal to Today");
            res.Add(mss);

        }
        return res; 
    }
}