Validation required with if ViewModel [duplicate]

-2

I'm trying to validate fields with the ViewModel This is not working:

 [Display(Name = "Insc. Estadual")]
public string InscricaoEstadual { get; set; }

[Display(Name = "Inscrição Isento")]
public bool InscricaoIsento { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    if (InscricaoIsento == false && string.IsNullOrEmpty(InscricaoEstadual))
    {
        yield return new ValidationResult("O campo Insc. Estadual é obrigatorio.");
     }
}

If the InscricaoIsento field is false and InscricaoEstadual is empty, it informs you that the field is required. This function is not working, I can not understand why, it is in ViewModel.

    
asked by anonymous 28.09.2018 / 19:48

2 answers

-1

Friend, try doing this:

[Display(Name = "Insc. Estadual")]
[Required]
public string InscricaoEstadual { get; set; }

[Display(Name = "Inscrição Isento")]
[Required]
public bool InscricaoIsento { get; set; }

Including [Required] in your fields.

    
28.09.2018 / 19:51
-1

Is your view model inheriting from IValidatableObject ? Try this:

public class Empresa : IValidatableObject
    
28.09.2018 / 20:23