0

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

}

wooters
  • 829
  • 6
  • 24
Ricardo Sousa
  • 37
  • 1
  • 1
  • 5
  • If I understand correctly, there are many pages, and each page has many files. So you'll need a link between the page content tables and file tables. For example, a foreign key. Then build up a view model from a linq query joining all of tables. – wooters Mar 21 '15 at 19:08
  • Yes, its that. All the queries are working fine, but i can´t show the values – Ricardo Sousa Mar 21 '15 at 19:16
  • Are you getting an error? Or can you just not see the file information on the page? Your viewbag code looks correct. – wooters Mar 21 '15 at 20:11
  • I´m getting the values on ViewBag. But i cant show them. My viewbag is { idfilec = 2, filenamec = 1qmifxxsbitk0ajnlohwy5r1cover_letter.docx, fileurlc = 1qmifxxsbitk0ajnlohwy5r1cover_letter.docx, fileimg = uleh4xscnt2dlnam0z4rxccbWord.png, fileextc = .doc }. How can I show them in the page separately? – Ricardo Sousa Mar 21 '15 at 22:28

1 Answers1

0

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...
    }
}
Community
  • 1
  • 1
wooters
  • 829
  • 6
  • 24