可以Html.Display / Html.DisplayFor / Html.DisplayForModel有一个DataTable的工作?有一个、工作、Display、Html

2023-09-06 22:43:12 作者:←長得太帥忚四①種檌√

我已经看到了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>
< /表>
 

html5display属性,css中display属性的介绍

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>