Show data from multiple models in the view

Asked

Viewed 100 times

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);
    }

Then I have a Join Inner to bring the data, but at the end in the select part I can only select the first table, and when I put so:

    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 use this?

  • Use a ViewModel

  • Explain better, because I direct my view when creating to a specific model, but how do I specify more than 1 model?

1 answer

3


The quick way to resolve (though slow in execution and fragile) is by defining the View as follows:

@model IEnumerable<dynamic>

Only this way there is no checking of types in design time, and the dynamic approach is always slower than the static reference.

Using a Viewmodel, 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 the View:

@model IEnumerable<MeuViewModel>
  • Thank you! You helped a lot.

Browser other questions tagged

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