What is the best technique for using ViewModel with mvc C # lists?

2

I have two templates: event registration package and subscription package, where the template event registration package has a list of subscription packages (see the code below, I removed some fields).

//Model
public class PacoteInscricaoEvento
{
    public PacoteInscricaoEvento()
    {
        PacoteIncricao = new List<PacoteIncricao>();
    }
    public int PacoteInscricaoEventoId { get; set; }
    public short Nrvagas { get; set; }       
    public IList<PacoteIncricao> PacoteIncricao { get; set; }
}

public class PacoteIncricao
{
    public int PacoteIncricaoId { get; set; }
    public string NmpacoteIncricao { get; set; }
}
So I created two views models, one for each model (code below), among other methods implements the abstract copy and view model copy methods of the abstract ViewModel class to facilitate the conversion process between the model view for model and vice- versa.

public class PacoteInscricaoEventoViewModel : ViewModel<PacoteInscricaoEventoViewModel, PacoteInscricaoEvento>
{
    public PacoteInscricaoEventoViewModel()
    {
        PacoteIncricaoViewModel = new List<PacoteIncricaoViewModel>();    
    }

    [Display(Name = "Código")]
    public int PacoteInscricaoEventoId { get; set; }
    [Required]
    [Display(Name = "Nº de Vagas")]
    [Range(1, int.MaxValue)]
    public short Nrvagas { get; set; }
    public List<PacoteIncricaoViewModel> PacoteIncricaoViewModel { get; set; }
    public override PacoteInscricaoEvento CopiarModelo(PacoteInscricaoEvento modelo)
    {
        int indiceElemento = 0;
        modelo.PacoteInscricaoEventoId = this.PacoteInscricaoEventoId;
        modelo.Nrvagas = this.Nrvagas;
        //listas
        //Excluindo os pacotes que foram exluidos na viewmodel no modelo
        if (modelo.PacoteIncricao != null && modelo.PacoteIncricao.Count > 0)
        {
            foreach (PacoteIncricao pacoteIncricaoModelo in modelo.PacoteIncricao)
            {
                //buscando o indice na view model
                indiceElemento = this.PacoteIncricaoViewModel.FindIndex(x => x.PacoteIncricaoId == pacoteIncricaoModelo.PacoteIncricaoId);
                if (indiceElemento == -1)
                    modelo.PacoteIncricao.Remove(pacoteIncricaoModelo);//se nao encontrar exclui
            }
        }
        //adcionando ou alterando os elementos nas view models
        if (this.PacoteIncricaoViewModel != null && this.PacoteIncricaoViewModel.Count > 0)
        {
            foreach (PacoteIncricaoViewModel pacoteInscricaoVM in this.PacoteIncricaoViewModel)
            {
                //buscando o indice no modelo
                indiceElemento = modelo.PacoteIncricao.ToList().FindIndex(x => x.PacoteIncricaoId == pacoteInscricaoVM.PacoteIncricaoId);
                if (indiceElemento == -1) //se nao encontrou altera
                    modelo.PacoteIncricao.Add(pacoteInscricaoVM.CopiarModelo(new PacoteIncricao()));
                else //senao altera o elemento
                    pacoteInscricaoVM.CopiarModelo(modelo.PacoteIncricao[indiceElemento]);
            }
        }
        return modelo;
    }
    public override PacoteInscricaoEventoViewModel CopiarView(PacoteInscricaoEvento modelo)
    {
        this.PacoteInscricaoEventoId = modelo.PacoteInscricaoEventoId;
        this.Nrvagas = modelo.Nrvagas;
        //listas
        foreach (PacoteIncricao pacoteinscricaotmp in modelo.PacoteIncricao)
        {
            this.PacoteIncricaoViewModel.Add(new PacoteIncricaoViewModel().CopiarView(pacoteinscricaotmp));
        }
        return this;
    }
}

public class PacoteIncricaoViewModel : ViewModel<PacoteIncricaoViewModel, PacoteIncricao>
{  
    [Display(Name = "Código")]
    public int PacoteIncricaoId { get; set; }
    [Required]
    [Display(Name = "Nome Pacote")]
    [StringLength(50)]
    public string NmpacoteIncricao { get; set; }             
    public override PacoteIncricao CopiarModelo(PacoteIncricao modelo)
    {
        modelo.PacoteIncricaoId = this.PacoteIncricaoId;
        modelo.NmpacoteIncricao = this.NmpacoteIncricao;
        return modelo;
    }   
    public override PacoteIncricaoViewModel CopiarView(PacoteIncricao modelo)
    {
        this.PacoteIncricaoId = modelo.PacoteIncricaoId;
        this.NmpacoteIncricao = modelo.NmpacoteIncricao;
        return this;
    }
}

So, what is the best technique (or code samples) for creating views models that contain views models in this situation (lists), without using automapper or similar? Thanks

    
asked by anonymous 09.01.2018 / 13:37

0 answers