我已经看到了this问题如何在一个视图中显示一个数据表
但不知道是否 Html.Display
或 Html.DisplayFor
可以作出与工作一个数据表
?
注:
使用 DataTable的
需要为这个项目。
我通过创建一个文件夹,名为DisplayTemplates在我查看文件夹的局部视图中创建一个DisplayTemplate。我所谓的局部视图表。它使用的模型是一个数据表,以下为code。在你看来,你就可以做 @ Html.DisplayForModel(表)
@using System.Data这;
@model数据表
<表ID =榜样级=显示>
< THEAD>
&其中; TR>
@ {
的foreach(COL的DataColumn在Model.Columns)
{
<第i个@ col.ColumnName
< /第i个
}
}
< / TR>
< / THEAD>
< TBODY>
@ {
的foreach(DataRow的行Model.Rows)
{
&其中; TR>
@ {的foreach(DataColumn的山坳中Model.Columns)
{
< TD> @row [COL]
< / TD>
}
}
< / TR>
}
}
< / TBODY>
< /表>
I have seen this question on how to display a DataTable
in a view but was wondering if Html.Display
or Html.DisplayFor
could be made to work with a DataTable
?
NB:
Use of DataTable
is required for this project.
解决方案
I created a DisplayTemplate by creating a partial view under my View folder in a folder called DisplayTemplates. I called the partial view Table. The model it uses is a datatable and below is the code. In your view you can then do @Html.DisplayForModel("Table")
@using System.Data;
@model DataTable
<table id="example" class="display">
<thead>
<tr>
@{
foreach (DataColumn col in Model.Columns)
{
<th>@col.ColumnName
</th>
}
}
</tr>
</thead>
<tbody>
@{
foreach (DataRow row in Model.Rows)
{
<tr>
@{foreach (DataColumn col in Model.Columns)
{
<td>@row[col]
</td>
}
}
</tr>
}
}
</tbody>
</table>