What is the best way to save a Group object that has lists of Items?

4

I have a registration and there are several links, for example:

public class Grupo {
   public string Nome {get;set;}
   public List<GrupoItemA> ItensA {get;set;}
   public List<GrupoItemB> ItensB {get;set;}
   public List<GrupoItemC> ItensC {get;set;}
}
public class GrupoItemB {
   public Grupo Grupo {get;set;}
   public ItemA Item {get;set;}
   public string Observacao {get;set;}
}

I have defined my classes.

My question is, I want the user to be able to link all the items in the View and only when sending the POST to save it to save everything, that is, save the Group and then the Item classes.

What would be the best way to do this?

I thought of Session, but they told me that it is not good practice and can be cumbersome if there are too many items.

I thought I would store everything in Array in Javascript, but if I give it an error or something, it will lose everything.

Or also in some static class

How could I get a better result in this case?

    
asked by anonymous 25.09.2014 / 14:25

3 answers

2

My question is, I want the user to be able to link all the items in the View and only when they send the POST to save it save everything, that is, save the Group and then the Item classes. What would be the best way to do this?

Writing HTML in the View. But it can not be any HTML.

For this answer, I'm assuming you know how to use BeginCollectionItem , a NuGet package designed to handle master- detail. You have 4 answers about me:

Another thing is that it is not a good practice to use List because List is an object with limited properties. The recommended way to use Model is:

[DisplayColumn("Nome")]
public class Grupo 
{
    [Key]
    public int GrupoId { get; set; }
    public string Nome { get; set; }

    public ICollection<GrupoItemA> ItensA {get;set;}
    public ICollection<GrupoItemB> ItensB {get;set;}
    public ICollection<GrupoItemC> ItensC {get;set;}
}

If there are any more questions, please let me know what you have answered.

    
30.09.2014 / 20:55
3

You need to get Group and Group Items data when the user posts.

I do not know if it's the best, but an alternative (which I've used several times) for this is you can create a Model (like GroupViewModel for example) that will have list / arrays properties to receive the Ids of each list of items that the user has selected for the Group, in addition to Group data (with Id, Name, etc.). .) and the name of the items, etc.

Example Model :

public class GrupoViewModel
{           
    private IRepositorioItem _repositorioItem;

    public GrupoViewModel(IRepositorioItem repositorioItem)
    {
        _repositorioItem = repositorioItem;
    }

    public GrupoViewModel():this()
    {
        PreencherListaDeGrupoItemA();
        PreencherListaDeGrupoItemB();
        PreencherListaDeGrupoItemC();
    }

    //Dados do Grupo
    public int ID { get; set; }
    public string Nome { get; set; }

    //Outros campos do seu model.....

    //Lista de Itens
    public int[] IdsGrupoItemA { get; set; }
    public int[] IdsGrupoItemB { get; set; }
    public int[] IdsGrupoItemC { get; set; }

    private void PreencherListaDeGrupoItemA()
    {
        //Código para buscar e preencher os itens IdsGrupoItemA 
        foreach(var item in _repositorioItem.ObterTodosGrupoItemA())
        {
           //Preenche os dados desejados como IdsGrupoItemA do item, etc...
        }
    }

    private void PreencherListaDeGrupoItemB()
    {
        //Código para buscar e preencher os itens IdsGrupoItemB 
        foreach(var item in _repositorioItem.ObterTodosGrupoItemB())
        {
           //Preenche os dados desejados como IdsGrupoItemB do item, etc...
        }
    }

    private void PreencherListaDeGrupoItemC()
    {
        //Código para buscar e preencher os itens IdsGrupoItemC 
        foreach(var item in _repositorioItem.ObterTodosGrupoItemC())
        {
           //Preenche os dados desejados como IdsGrupoItemC do item, etc...
        }
    }
}

In Controller , Action , you create an instance of GrupoViewModel by filling in the information to be displayed (Group data and item lists).

To mount your View (of type GroupViewModel ) use the item ids, which will already be filled in, using checkbox (which I usually use) with the same name as the properties for the user to select the desired items.

In Controller , in your Action Post , you receive the Ids of items selected by the user to each Id you retrieve the object (be it GroupItemA, GroupItemB and / or GroupItemC) and associates / adds in the item list of your Group.

Example Controller :

public class GrupoController
{
    public ActionResult Create()
    {                    
       return View(new GrupoViewModel());
    }

    [HttpPost]
    public ActionResult Create(GrupoViewModel grupoViewModel)
    {
        ...
        List<GrupoItemA> listaItensAselecionados = new List<GrupoItemA>();
        foreach(var idGrupoItemA in grupoViewModel.IdsGrupoItemA)
        {
            //Recupero o objeto GrupoItemA desse Id e adiciono na lista listaItensAselecionados 
        }

        List<GrupoItemB> listaItensBselecionados = new List<GrupoItemB>();
        foreach(var idGrupoItemB in grupoViewModel.IdsGrupoItemB)
        {
            //Recupero o objeto GrupoItemB desse Id e adiciono na lista listaItensBselecionados 
        }

        List<GrupoItemC> listaItensCselecionados = new List<GrupoItemC>();
        foreach(var idGrupoItemC in grupoViewModel.IdsGrupoItemC)
        {
            //Recupero o objeto GrupoItemC desse Id e adiciono na lista listaItensCselecionados 
        }

        ...

        //Agora com os itens selecionados recuperados você cria o Grupo novo
    }
}
    
25.09.2014 / 17:26
1

Two good strategies would be to use (1) lightweight objects and / or (2) caches.

  • Light objects are objects filled only with the identification and display fields for the user, for example "id" and "description."

  • Cache is a form of temporary information storage and quick access. For .Net you can read about caching here: link

  • 25.09.2014 / 16:15