Show data from multiple models in the view

3

I have the following problem: In a view I will show data from 8 different tables and I have the following code that I put together to take the data to the view.

 public ActionResult Index()
    {
        var avisos = (from av in neEAD.mot_avisos
                      join tm in neEAD.mot_turma on av.av_tm_id equals tm.tm_id
                      join ta in neEAD.mot_turmaaluno on tm.tm_id equals ta.ta_tm_id
                      join al in neEAD.mot_aluno on ta.ta_al_id equals al.al_id
                      join tmp in neEAD.mot_turmaprofessor on tm.tm_id equals tmp.tp_tm_id
                      join pf in neEAD.mot_professor on tmp.tp_pf_id equals pf.pf_id
                      join pfd in neEAD.mot_professor_disciplina on pf.pf_id equals pfd.pfd_pf_id
                      join dp in neEAD.mot_disciplina on pfd.pfd_dp_id equals dp.dp_id
                      where (dp.dp_id == 1 && tm.tm_id == 4 && al.al_id == 22)
                      orderby av.av_datacadastro descending
                        select av).ToList();
        return View(avisos);
    }

So I have an inner join to bring the data, but in the end in the select part I can only select the first table, and when I put it like this:

    select new {
          av.av_datacadastro,
          av.av_mensagem,
          av.av_titulo,
          pf.pf_nome
   }

My error code in the view. Is there any way I can put more than one model in the view? How do I do this inner join?

    
asked by anonymous 17.05.2016 / 21:35

1 answer

3

The quick way to resolve (but slow at execution and fragile) is by setting View as follows:

@model IEnumerable<dynamic>

But in this way there is no type checking at design time, and the dynamic approach is always slower than the static reference.

Using a ViewModel , it would look like this:

public ActionResult Index()
{
    var avisos = (from av in neEAD.mot_avisos
                  join tm in neEAD.mot_turma on av.av_tm_id equals tm.tm_id
                  join ta in neEAD.mot_turmaaluno on tm.tm_id equals ta.ta_tm_id
                  join al in neEAD.mot_aluno on ta.ta_al_id equals al.al_id
                  join tmp in neEAD.mot_turmaprofessor on tm.tm_id equals tmp.tp_tm_id
                  join pf in neEAD.mot_professor on tmp.tp_pf_id equals pf.pf_id
                  join pfd in neEAD.mot_professor_disciplina on pf.pf_id equals pfd.pfd_pf_id
                  join dp in neEAD.mot_disciplina on pfd.pfd_dp_id equals dp.dp_id
                  where (dp.dp_id == 1 && tm.tm_id == 4 && al.al_id == 22)
                  orderby av.av_datacadastro descending
                  select new MeuViewModel {
                      DataCadastro = av.av_datacadastro,
                      Mensagem = av.av_mensagem,
                      Titulo, av.av_titulo,
                      Nome = pf.pf_nome
                  }).ToList();

    return View(avisos);
}

The ViewModel is a class that can be defined like this:

public class MeuViewModel
{
    public DateTime DataCadastro { get; set; }
    public String Mensagem { get; set; }
    public String Titulo { get; set; }
    public String Nome { get; set; }
}

And View :

@model IEnumerable<MeuViewModel>
    
17.05.2016 / 21:56