I had View strongly type of my class Usario Defined like this
public partial class Usuario
{
public Usuario()
{
this.Campana = new HashSet<Campana>();
}
public int IDUsuario { get; set; }
public int IDPerfil_FK { get; set; }
public string Nombre { get; set; }
public string Password { get; set; }
public bool Activo { get; set; }
public virtual Perfil Perfil { get; set; }
public virtual ICollection<Campana> Campana { get; set; }
}
Now what i want to know is how can I avoid the validations of the class campana which is related with the class Usuario in the view, because when I doing ModelState.IsValid the Model validates the attributes of the class Usuario and also of class the Campana
The correct way of doing this is to use view models.
You already have
Usuarioclass, now you implement the view model, which will only hold the properties you want to pass to the view. Something like:Now on the controller:
This way you only pass to the view the needed data. Data annotations work for view models exactly the same way as any other class.
modelis initialized by MVC and properties are filled in with incoming data, through routeValues, so you can do something like this: