MVC 5应用程序-代码优化帮助

时间:2018-07-30 19:17:48

标签: asp.net-mvc

我有一个html页面,我需要用存储在datatable中的数据呈现3个表。我从模型中提取数据,然后用html编写视图。我有3个不同的数据表-主数据表,辅助数据表等。我在表代码中唯一更改的是模型名称(以下主要代码)。如何优化此方法,而不是分别重写所有3个代码? TIA为您提供帮助。

<table id="primary" class="display" style="overflow:hidden !important;">
        @if (Model.PrimaryFunction != null)
        {
            <thead>
                <tr>
                    @foreach (DataColumn column in (Model.PrimaryFunction as DataTable).Columns)
                    {
                        @if (column.ColumnName.ToUpper().Contains("STEP TITLE"))
                        {
                            <th style="max-width:200px;">@column.ColumnName.ToUpper()</th>
                        }
                        else if (column.ColumnName.ToUpper().Contains("STEP #"))
                        {
                            <th style="white-space:nowrap;">@column.ColumnName.ToUpper()</th>//header row
                        }
                        else
                        {
                            <th class="select-filter">@column.ColumnName.ToUpper()</th>//header row
                        }
                    }
                </tr>
            </thead>
            <tbody>
                @if ((Model.PrimaryFunction as DataTable).Rows.Count > 0)
                {
                    foreach (DataRow dr in (Model.PrimaryFunction as DataTable).Rows)
                    {
                        int count = 0;
                        <tr>
                            @foreach (DataColumn column in (Model.PrimaryFunction as DataTable).Columns)
                            {
                                if (count < 2)
                                {
                                    <td style="background-color:antiquewhite;">
                                        @dr[column].ToString()
                                    </td>//write one row at a time.
                                }
                                else
                                {
                                    <td style="text-align:center !important;">
                                        @dr[column].ToString()
                                    </td>//write one row at a time.
                                }
                                count++;

                            }
                        </tr>
                    }
                }
                else
                {
                    var count = (Model.PrimaryFunction as DataTable).Columns.Count;
                    <tr>
                        <td colspan='@count' style="color:red;">
                            No Data Found.
                        </td>
                    </tr>
                }
            </tbody>

        }
        else
        {
            <tr>
                <td style="color:red;">
                    @(Model.PrimaryFunction.HasErrors != false ? Model.PrimaryFunction.HasErrors.ToString() : "")
                </td>
            </tr>
        }
    </table>

1 个答案:

答案 0 :(得分:0)

我使用了不同的方法。使用局部视图..将参数传递给局部视图,并在主视图中调用它。这样也解决了。

<div id="table1">
    @Html.Partial("_PopulateTable", new ViewDataDictionary(ViewData) { { "table", "table1" } })

</div>
<div id="table2">
    @Html.Partial("_PopulateTable", new ViewDataDictionary(ViewData) { { "table", "table2" } })

</div>
<div id="table3">
    @Html.Partial("_PopulateTable", new ViewDataDictionary(ViewData) { { "table", "table3" } })
</div>