将多个值从查询传递到视图

Passing multiple values from query to view

如何将 Linq select 中的值从多个表传递到视图?我有一个显示页面内容的模型,并且我查询 select 来自该内容的所有文件。文件信息由多个值定义。怎样才能在View中看到?我必须创建一个 ViewModel 吗?

我的控制器

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

我的观点:

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

}

您正在创建匿名类型列表并将其分配给 ViewBag "fichconte" 属性。列表中的结果对象是内部的,并且不适用于动态 ViewBag。请参阅 this post 了解类似的场景。

我会创建一个 ViewModel class 来包装您当前的模型以及您分配给 ViewBag 的列表。

型号:

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
}

控制器:

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

查看:

@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...
    }
}