2
In my project I currently have 3 tables: Students, Occurrences and Users. In them I have the views and the controllers.
So far, so good. My relationships are: Allunos&occurrences(1,N) / Occurrences & users(N,1).
My problem is that in action Details, both of Occurrences and of Users, I can not show some relationship information. In the case of occurrences I cannot show the name of the student or the user (but in the Index, I can show these names), and in the case of users I cannot show the name of the student (which would have to be shown by the fact that it comes from the relationship of Alunos&occurrences).
Here’s my code that’s on action which shows all information, except those mentioned above:
Actionresult of Controller Occurrences
public ActionResult Detalhes(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
if (ocorrencia == null)
{
return HttpNotFound();
}
return View(ocorrencia);
}
Actionresult of Controller Users
public ActionResult Detalhes(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//Usuario usuario = db.Usuarios.Find(id);
Usuario usuario = db.Usuarios.Include(o => o.Ocorrencias).AsNoTracking().FirstOrDefault(u => u.UsuarioID == id);
if (usuario == null)
{
return HttpNotFound();
}
return View(usuario);
}
Could someone help me ? This is the case of a Join ?
Gypsy, in the case of occurrences worked well, but in the case of the user did not work. Just to be clear, the student only relates to the occurrence and the occurrence relates to both students and users. So, how can I get the name of the student who is on the occurrence and show him when fetching user details ?
– Érik Thiago
@Érikthiago The occurrence would be
@ocorrencia.Usuario.Nome
.– Leonel Sanches da Silva
That way I know Gypsy. But how the name of the student and show in the table of users ? I think I was clearer.
– Érik Thiago
@Érikthiago Actually it was not very clear, but I think I understood. If you want to carry the student’s name from the occurrence, it is
@ocorrencia.Aluno.Nome
.– Leonel Sanches da Silva
Ok. What happens is that when I detail the occurrences related to a login the student’s name is not shown. And I wanted to show the student’s name in the user details view. Because I want to show my user the occurrences that he generated and for whom, understood ? This is what is happening.
– Érik Thiago
Then you use
db.Usuarios.Include(u => u.Ocorrencias.Select(o => o.Aluno))
.– Leonel Sanches da Silva
Very good Gypsy, it worked here ! Thanks a lot !
– Érik Thiago