I have these 2 properties in a model
public class Geometria
{
public int Id { get; set; }
public string Componente { get; set; }
[Range(0, float.MaxValue)]
public float ToleranciaInferior { get; set; }
[Range(0,float.MaxValue)]
public float ToleranciaSuperior { get; set; }
}
The property ToleranciaSuperior cannot be the same or equal as ToleranciaInferior.
How can i achieve this with annotations?
It'd be more convenient to put custom validation logic in the viewmodel itself, unless you find yourself doing this on more than one viewmodel.
public class Geometria : IValidatableObject
{
public int Id { get; set; }
public string Componente { get; set; }
[Range(0, float.MaxValue)]
public float ToleranciaInferior { get; set; }
[Range(0,float.MaxValue)]
public float ToleranciaSuperior { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (ToleranciaInferior == ToleranciaSuperior)
{
yield return new ValidationResult(
"Your error message",
new string[] {
nameof(ToleranciaInferior), nameof(ToleranciaSuperior)
});
}
}
}