GreaterThan and LessThan as DataAnnotation on the same Attribute

2.5k Views Asked by At

i'm trying to do a condition on a attribute having DateTime as a type,it has to be greaterThan another attribute of the same Object and lessThan another which is also an attribute of the same Object.My probleme is when i try to apply these two DataAnnotation it gives me this error:

Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: is

i tried this after struggling with Range DataAnnotation,i know it's not the best way to do this,but Range just did'nt worked for me it always given me this error:

The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

here is my model(i'm using entity framework to generate my models so i'm using partialclasses to do validation tricks):

 [MetadataType(typeof(AffectationMetadata))]
public partial class AFFECTATION
{

    public DateTime DateDebutVisite { set; get; }

    public DateTime DateFinVisite { set; get; }



    public class AffectationMetadata
    {
        //some other entities

        [Required(ErrorMessage = "Date de début de visite est obligatoire.")]
        [Display(Name = "Date du projet dans la période de la visite * :")]
        [GreaterThan("DateDebutVisite")]
        [LessThan("DateFinVisite")]
        public DateTime DATEAFFECTATION { set; get; }


    }
}

Please can someone tell me how can i solve this problem?

1

There are 1 best solutions below

0
On

So there are 2 problems here. Firstly Foolproof validation can only see properties in the same class as itself. So your class would need to look like this:

[MetadataType(typeof(AffectationMetadata))]
public partial class AFFECTATION
{
    public class AffectationMetadata
    {
        public DateTime DateDebutVisite { set; get; }

        public DateTime DateFinVisite { set; get; }

        //some other entities

        [Required(ErrorMessage = "Date de début de visite est obligatoire.")]
        [Display(Name = "Date du projet dans la période de la visite * :")]
        [GreaterThan("DateDebutVisite")]
        [LessThan("DateFinVisite")]
        public DateTime DATEAFFECTATION { set; get; }


    }
}

The second problem and this is really the crux of the whole thing is that GreaterThanAttribute and LessThanAttribute are actually just child classes of the IsAttribute as a result it sees 2 of the same attribute on the same property which throws an error. I'm currently looking for a solution to that problem myself. I'll edit this answer if I find one.