可以Html.Display / Html.DisplayFor / Html.DisplayForModel有一个DataTable的工作? [英] Can Html.Display/Html.DisplayFor/Html.DisplayForModel work with a DataTable?
问题描述
我已经看到了<一个href="http://stackoverflow.com/questions/4015025/how-do-i-display-a-datatable-in-asp-net-mvc-2">this问题如何在一个视图中显示一个数据表
但不知道是否 Html.Display
或 Html.DisplayFor
可以作出与工作一个数据表
?
注:
使用
DataTable的
需要为这个项目。
我通过创建一个文件夹,名为DisplayTemplates在我查看文件夹的局部视图中创建一个DisplayTemplate。我所谓的局部视图表。它使用的模型是一个数据表,以下为code。在你看来,你就可以做 @ Html.DisplayForModel(表)
@using System.Data这;
@model数据表
&LT;表ID =榜样级=显示&GT;
&LT; THEAD&GT;
&其中; TR&GT;
@ {
的foreach(COL的DataColumn在Model.Columns)
{
&LT;第i个@ col.ColumnName
&LT; /第i个
}
}
&LT; / TR&GT;
&LT; / THEAD&GT;
&LT; TBODY&GT;
@ {
的foreach(DataRow的行Model.Rows)
{
&其中; TR&GT;
@ {的foreach(DataColumn的山坳中Model.Columns)
{
&LT; TD&GT; @row [COL]
&LT; / TD&GT;
}
}
&LT; / TR&GT;
}
}
&LT; / TBODY&GT;
&LT; /表&gt;
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>
这篇关于可以Html.Display / Html.DisplayFor / Html.DisplayForModel有一个DataTable的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!