List Entity Framework

0

I am creating action from Edit to edit the data of my models . What happens is that I use ViewModel to "join" several models into one.

Okay, I can get data from models which are not List<> , ie models that are not a list. My case is that I have lists and needed to get all the records for the id I'm passing in the Action signature so that the data can be loaded onto the screen, and edit that data. >

The code I have is this one:

 CliCliente cliente = db.CliCliente.Find(id);
 Tabela2 tabela2 = db.Tabela2.Find(id);
 Tabela3 tabela3 = db.Tabela3.Find(id);

 List<Tabela4> tabela4 = db.Tabela4.ToList<Tabela4>();

How could I do to scan and bring the specific list data related to the List<Tabela4> tabela4 = db.Tabela4.ToList<Tabela4>(); sent in the signature of the action ?

    
asked by anonymous 09.06.2016 / 05:32

1 answer

0

Here you mentioned how the relationships between your tables . Assuming this, I can do the following:

CliCliente cliente = db.CliCliente
                       .Include(c => c.Tabela2)
                       .Include(c => c.Tabela3)
                       .Include(c => c.Tabela4)
                       .FirstOrDefault(c => c.CliClienteId == id);

return View(cliente);

Your View :

@model SeuProjeto.Models.CliCliente

@foreach (var elemento in Model.Tabela4)
{
    <p>@elemento.Nome</p>
}
    
10.06.2016 / 23:41