How can I pass values from a Linq select from multiple tables to a view? I have a model that shows the content of a page, and I have a query to select all files from that content. The files information is defined by multiple values. How can be seen in the View? Do I have to create a ViewModel?

My controler

    public ActionResult Index()
    {
        ViewBag.Message = "Academia Page";

        var cwc_academia = db.CWC_CONTEUDOS.Include(c => c.CWC_PAGINAS)
                             .Where(c => c.CWC_PAGINAS.id_page == 1);

        var ficheirosconteudos = (from c in db.CWC_FILESCONTEUDOS
                    join d in db.CWC_FICHEIROS on c.idfile equals d.id_file
                    join e in db.CWC_TIPOSFICHEIROS on d.idfiletype equals e.id_tpfile
                    join f in db.CWC_EXTENSOESFILES on e.id_tpfile equals f.idtpdoc
                    select (new 
                          {
                              idfilec = d.id_file,
                              filenamec = d.filename,
                              fileurlc = d.fileurl,
                              fileimg = e.tipoimg,
                              fileextc = f.extensao
                          })).ToList();

        ViewBag.fichconte = ficheirosconteudos;

        return View(cwc_academia.ToList());
    }

and my view:

@foreach (var item in Model)
{
   <div class="divider"><div class="circle">
      <img src="/Images/orange.png" alt="" /></div></div>
        <div id="acad" class="container">
            <div class="jumbotron">
                <h2>@Html.Raw(item.conttitle)</h2>
                @Html.Raw(item.conttext)
            </div>
        </div>

if (ViewBag.fichconte != null){

    foreach (var fich in ViewBag.fichconte)
    {
        @fich.idfilec
        <br />
        @fich.filenamec
        <br />
        @fich.fileurlc
        <br />
        @fich.fileimg
        <br />
        @fich.fileextc
        <br />
    }
}

}

Related posts

Recent Viewed