Send a list of data in a viewbag and receive a foreach in the view

1

The query data is correct, but I can not return the data to the view.

Controller:

var disponib = (from d in db.Disponibilidade
                join c in db.Catequista on d.CatequistaID equals c.CatequistaID
                where c.CatequistaID == d.CatequistaID && d.CatequistaID == id
                select new
                {
                    AnoPastoral = d.AnoPastoral,
                    DiaDisponivel = d.DiaDisponivel,
                    HoraDisponivel = d.HoraDisponivel,
                    HoraFim = d.HoraFim,
                    Observacoes = d.Observacoes,
                }).ToList();

ViewBag.Disponibilidade = disponib;

View:

@model WebAppCatechesis2.Models.Catequista

@foreach (var item in ViewBag.Disponibilidade)
{    
    //  ???
}
    
asked by anonymous 23.03.2016 / 16:15

2 answers

1

Problem solved with query

var disponib = (from d in db.Disponibilidade
                            join c in db.Catequista on d.CatequistaID equals c.CatequistaID
                            where c.CatequistaID == d.CatequistaID && d.CatequistaID == id
                            select d);

            ViewBag.Disponibilidade = disponib;
    
23.03.2016 / 17:35
1
var lista = linq.toList<suaClasse>();
ViewBag.MinhaView = lista;


@foreach (var item in ViewBag.MinhaView as IList<suaClasse>) {
   //-- Codigo
}

linq = (your query expression)

And feasible you put before foreach a type check:

@if(ViewBag.MinhaView != null) {
   //-- foreach aqui
}

I do not know about your application, but there is how you can pass an object to the view maybe even better:

return PartialView("NomeDaView", ObjetoDeClasse);

in View accesses with

@model ObjetoDeClasse

@foreach(var item in Model){ 
   //-- Codigo
}

I hope to help.

    
23.03.2016 / 16:52