Lists do not load next to model

Asked

Viewed 39 times

1

My model:

public class Grupo: ModelBase
  {
    public Grupo()
    {
      this.Itens = new List<Item>();
    }

    public Grupo(string nome): this()
    {
      this.Nome = nome;
    }

    public string Nome { get; set; }
    public virtual ICollection<Item> Itens { get; set; }

  }

My Pository:

public virtual T BuscaPorId(int id)
{
  return _dbSet.Find(id);
}

And my way into my Service class

public Grupo BuscarPorId(int id)
{
  return grupoRepository.BuscaPorId(id);
}

When I call in the controller it comes the lists that are in the "Group"

public ActionResult Editar(int Id)
{
  if (Id == null)
  {
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  }
  Grupo grupo = grupoService.BuscarPorId(Id);

But when I click on save and pass to my Service calling my method this.BuscarPorId, exactly the same method I called in the controller, here it does not load the lists, only the data "Name,Id"

1 answer

2


Find does not guarantee charge of dependent entities here:

public virtual T BuscaPorId(int id)
{
    return _dbSet.Find(id);
}

Explicitly load items to ensure they return as desired:

public virtual T BuscaPorId(int id)
{
    return _dbSet.Include(g => g.Itens).SingleOrDefault(g => g.Id == id);
}

Browser other questions tagged

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