Required validating property of another object

2

I have the following properties, but when I am going to validate via ModelState.IsValid it returns that the description of the user group being informed that it is UserId too, but the problem is that I am prompting for the bank itself to generate the key. How do I not display these inconsistencies?
Usergroup is being populated 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; }
    
asked by anonymous 27.10.2015 / 01:40

1 answer

5

Do not use this:

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

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

[Required(ErrorMessage = "Grupo deve ser selecionado")]
public int GrupoDeUsuarioId { get; set; }
    
27.10.2015 / 01:48