Validation required with if Viewmodel

Asked

Viewed 90 times

-2

I’m trying to validate fields with Viewmodel This way it’s 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 field InscricaoIsento for false and InscricaoEstadual is empty, it informs that the field is mandatory. This function is not working, I can not understand why, is in ViewModel.

  • If you use the notation of [Required] it may suit you.

  • If the Entered field is false and the Entered field is empty, it informs that the field is mandatory. But if the Registration field is true, the Registration Status does not need to be filled Unfortunately it does not meet. and

  • This has not been answered here https://answall.com/questions/331767/condi%C3%A7%C3%A3o-if-com-required-viewmodel/331806#331806 ? Otherwise, put more information on this question, such as the full Model code and Controller Action so we can help.

  • @Renan it is not working.

  • Put more information on this question, such as the full Model code and Controller Action so we can help.

  • I just need the validation of the field, but as if as described, this way is not validating.

Show 1 more comment

2 answers

-1

Buddy, try it like 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.

  • If the Entered field is false and the Entered field is empty, it informs that the field is mandatory. But if the Entered field is true, the Entered State does not need to be filled in.

  • Ah, ok! Tebte do via javascript. Calling a validation at the Onclick event

-1

Your model view is inheriting from IValidatableObject? Try it like this:

public class Empresa : IValidatableObject
  • 1

    I did, but it still didn’t work.

  • In your controller, you have validated Modelstate.Validationstate?

  • He does the validation ModelState.IsValid

  • In order to test, try placing the affected fields in the validation in the constructor, like this: Yield Return new Validationresult("The Insc field. State is required." , new[] { "Inscribed "});

  • It is already so, as in the question.

  • I’m sorry, but in the question you are using a constructor with only one parameter, this being the error message, what I suggested is to use the two parameter constructor, informing the error message and the list of affected variables.

  • It’s because when I saw the comment was still the same, I took the test and it still didn’t work.

Show 2 more comments

Browser other questions tagged

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