Entity Framework List

Asked

Viewed 739 times

0

I am created to action of Edit to edit my data models. What happens is I use Viewmodel to "join" several models in a.

Okay, I can get the data from models who are not List<>, that is, the models that are not a list. My case is that I have lists and needed to get all the records relating to id that I’m signing at Action so that the data can be loaded on 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>();

In which List<Tabela4> tabela4 = db.Tabela4.ToList<Tabela4>(); returns all the data, because I don’t have a criterion to return only what is related in id that I’m bringing into the action.

How could I do to scan and bring the specific list data related to id sent in the signature of action?

  • If the id is of CliCliente and Tabela2, Tabela3 and Tabela4 are related, why don’t you just select from CliCliente and uses only Include()s to load the aggregated data?

  • I tried, but I couldn’t... And that way with include would come out a list? Because I need to return a list...

  • Return to where?

  • for the view... And also in the controller

1 answer

0


Here you said how are the relationships between your tables. Assuming that, 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>
}

Browser other questions tagged

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