Search code examples
c#asp.net-mvclinqrazorviewbag

Passing multiple values from query to view


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 />
    }
}

}


Solution

  • You are creating a List of anonymous types and assigning it to the ViewBag "fichconte" property. The resulting objects in the list are internal, and that is not going to work with the dynamic ViewBag. Please see this post for a similar scenario.

    I would create a ViewModel class to wrap both your current model as well as the List that you are assigning to the ViewBag.

    Models:

    public class AcademiaViewModel
    {
        public List<CwcConteudos> CwcConteudos {get; set;}
        public List<Ficheiroconteudos> FicheiroconteudosList {get; set;}
    }
    public class Ficheiroconteudos 
    {
        public string IdFilic {get; set;}
        ...add the rest of the properties of the anonymous type
    }
    

    Controller:

    public ActionResult Index()
    {
        ViewBag.Message = "Academia Page";
        AcademiaViewModel viewmodel = new AcademiaViewModel();
    
        viewmodel.CwcConteudos = db.CWC_CONTEUDOS.Include(c => c.CWC_PAGINAS)
                             .Where(c => c.CWC_PAGINAS.id_page == 1);
    
        viewmodel.FicheiroconteudosList = (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 Ficheiroconteudos
                          { 
                              IdFilic = d.id_file,
                              filenamec = d.filename,
                              fileurlc = d.fileurl,
                              fileimg = e.tipoimg,
                              fileextc = f.extensao
                          })).ToList();
    
        return View(viewmodel);
    }
    

    View:

    @foreach (var item in Model.CwcConteudos)
    {
       <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 Model.FicheiroconteudosList)
        {
            fich.IdFilic
            <br />
            property2...
            <br />
            property3 etc...
        }
    }