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 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>
If you mapped your data table into an IEnumerable of an object, I believe DisplayFor would handle this.