View model with Icollection

Asked

Viewed 52 times

0

I’m starting in mobile development and I’m doing a category control system. Where I give possibility to do various levels of categories.

My entity was as follows:

public int CategoriaId { get; set; }
public string Descricao { get; set; }
public int? CategoriaPaiId { get; set; }
public virtual Categoria CategoriaPai { get; set; }
public virtual ICollection<Categoria> CategoriasFilho { get; set; }

My doubts are being raised ViewModel

I started programming and so far it’s like this:

    [Key]
    public int CategoriaId { get; set; }

    [Display(Name = "Categoria pai")]
    public string CategoriaPaiId { get; set; }


    [Display(Name = "Descrição da categoria")]
    [Required(ErrorMessage = "Descrição obrigatória.")]
    public string Descricao { get; set; }

    [Display(Name = "Ativo")]
    public bool Status { get; set; }

How can I conclude this ?

  • I don’t understand your question. Why you need to create a ViewModel?

  • Because here I use viewmodel to instantiate my object

  • Yes, but why? What is the point of this? A ViewModel is almost identical to Model. There’s no need for that if they’re the same, or almost the same.

  • I don’t know the correct explanation, as I said I’m new, in the standard project which I’m following he has two view models, which he calls Categoriaviewmodel and Managementcategory viewmodel, in it are the described data of my entity, I need to describe in it a list, of categories

  • Well, this pattern is long-winded and I believe you can simplify it. I will direct an answer without considering ViewModels, okay?

  • OK @Gypsy Rrisonmendez

Show 1 more comment

1 answer

1


The separation into Viewmodels is only required if the presentation of data (Views) is different from what is proposed in Model. That is, if your Model is like this:

public int CategoriaId { get; set; }
public string Descricao { get; set; }
public int? CategoriaPaiId { get; set; }
public virtual Categoria CategoriaPai { get; set; }
public virtual ICollection<Categoria> CategoriasFilho { get; set; }

You just need to decorate it like you did yours Viewmodel, and this can still be improved:

[Key]
public int CategoriaId { get; set; }

[Display(Name = "Categoria pai")]
public int? CategoriaPaiId { get; set; }

[Display(Name = "Descrição da categoria")]
[Required(ErrorMessage = "Descrição obrigatória.")]
public string Descricao { get; set; }

[Display(Name = "Ativo")]
public bool Status { get; set; }

public virtual Categoria CategoriaPai { get; set; }
public virtual ICollection<Categoria> CategoriasFilho { get; set; }

It’s okay to expose a Model in View. To protect which fields can be defined, use the attribute [Bind] with Include:

[HttpPost]
public async Task<ActionResult> Criar([Bind(Include = "Descricao,CategoriaPaiId")] Categoria categoria) { ... }

Or, if it’s easier, Exclude:

[HttpPost]
public async Task<ActionResult> Criar([Bind(Exclude = "CategoriaId,CategoriaPai,CategoriasFilho")] Categoria categoria) { ... }

Browser other questions tagged

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