Check two null fields with Data Annotation

Asked

Viewed 93 times

4

I own two properties:

public int? Inicio {get; set;}
public int? Final {get; set;}

Both accept null and are not required. I wanted to check if both are null using Data Annotation. The operation would be more or less similar to the Compare from Data Annotation itself., but checking that the value of the two properties are null.

2 answers

6

When it involves two fields, it is right to decorate your Model with IValidatableObject as follows:

public class MeuModel : IValidatableObject
{
    public int? Inicio {get; set;}
    public int? Final {get; set;}

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        /* Verifique aqui as variáveis.
           Em caso de problemas, devolva erros usando:
           yield return new ValidationResult("Mensagem de erro.", new[] { "CampoEnvolvido1", "CampoEnvolvido2" });
        */
    }
}

0

You can create a new Notation for this I sent below how to do you will overwrite the method is Valid

Browser other questions tagged

You are not signed in. Login or sign up in order to post.