在MVC中的View中嵌套for循环

时间:2014-11-18 12:41:10

标签: c# asp.net-mvc

我有两个数据表在MVC视图中加载了数据

DataTable dtGrpDisplays= ViewBag.SubGrpDisplayDocs as DataTable;
DataTable dtDownloads= ViewBag.DownloadRequirements as DataTable;

dtGrpDisplays has records - CityId , Display Document Path, Group Id
dtDownloads has records - CityId, Download document Path, Group Id

我需要首先显示“显示文档”,然后显示该组ID的下载文档。

在View中,我有以下代码来显示显示文档

@{  for (int i = 0; i < dtGrpDisplays.Rows.Count; i++)
  {
        <div class="form_header"><h3>   @dtGrpDisplays.Rows[i]["UploadTitle"].ToString() </h3></div>
        <div id ="@i">
        <iframe src="@dtGrpDisplays.Rows[i]["UploadPath"].ToString()" Id="@dtGrpDisplays.Rows[i]["Id"].ToString()"   class="iframe"  scrolling="no" width="100%" marginwidth ="0" marginheight="0" style="border:none;top:auto;"></iframe>

        </div>
  }
}

现在,我的问题,如何编写内部for循环以显示Datatable dtGrpDisplays中特定组ID的可下载文档。

我对MVC很新。请帮我解决一下。

这可能吗?

由于

1 个答案:

答案 0 :(得分:0)

var SubGrpDisplayDocs = (from m in dtGrpDisplays.AsEnumerable()                        
                                            select m).ToList();

var DtDownloads = (from m in DtDownloads.AsEnumerable()
                                    select m).ToList(); 

ViewBag.Data = from m in SubGrpDisplayDocs
                                join n in DtDownloads on m.GroupID equals n.GroupID
                                where m.GroupID = n.GroupID
                                select m;    

在应用程序的Controller(ActionResult)部分中执行此LINQ,然后使用Razor在View部件中执行forloop循环Viewbag。如果你不了解LINQ我建议你研究它。这将使查询更容易