Required validating property of another object

Asked

Viewed 108 times

2

I have the following properties, however when I will validate via Modelstate.Isvalid returns that the description of the user group to be informed that it is User too, but the problem is that I am asking for the database itself to generate the key. How do I not present these inconsistencies?
Groupuser is being filled with a Dropdownlist.

     [Key]
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int UsuarioId { get; set; }

    [Required(ErrorMessage = "Nome do usuário deve ser informado")]
    public string UsuarioDeAcesso { get; set; }

    [Required(ErrorMessage = "Senha do usuário deve ser informada")]
    public string SenhaDeAcesso { get; set; }

    [Required(ErrorMessage = "Grupo deve ser selecionado")]
    public int GrupoDeUsuarioId { get; set; }
    [Required(ErrorMessage = "Grupo deve ser selecionado")]
    public GrupoDeUsuario GrupoDeUsuario { get; set; }
  • One way I found was to use this.ModelState.Remove("User"); but I think for this case is not the most appropriate.

1 answer

5


Don’t use this:

[Required(ErrorMessage = "Grupo deve ser selecionado")]
public GrupoDeUsuario GrupoDeUsuario { get; set; }

Navigation properties are complex entities. You should only keep this validation completed below, because this is the important element of the validation, and not the dependent object:

[Required(ErrorMessage = "Grupo deve ser selecionado")]
public int GrupoDeUsuarioId { get; set; }
  • It worked, I’m used to Nhibernate, these navigation properties are new to me. But how do I not validate the user? no cadrastro he must be 0 same but the change must be informed.

  • The secret is to use IValidatableObject. Ask another question I’ll tell you how.

Browser other questions tagged

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