I have a simple Partial View that gets a db's schema (column names) from the controller in a ViewBag. I would like to then roll out the row data without having to specify the column names as I have done here, in order to make it "schema agnostic" like the ViewBag columns are. The controller SQL query to gather the column names is:
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Sales'"
The Partial View is here:
@model App.Models.AppModel
@{
Layout = null;
}
<table>
<thead>
<tr>
@{foreach (var ColumnName in ViewBag.DbColumnNames)
{
<th>@ColumnName</th>
}
</tr>
</thead>
<tbody>
@{foreach (var ColumnData in Model.Sales)
{
<tr>
<td>@ColumnData.ItemId</td>
<td>@ColumnData.Item</td>
<td>ColumnData.Description</td>
<td>@ColumnData.Qty</td>
<td>@ColumnData.Price</td>
<td>@ColumnData.LineTotal</td>
<td>@ColumnData.SaleNo</td>
<td>@ColumnData.Taxable</td>
<td>@ColumnData.Memo</td>
</tr>
}
}
</tbody>
</table>
Can't loop though it. I tried that. It doesn't contain a GetEnumerator. I have to make the db record Enumerable so I could foreach it. What I came up with is probably retarded and wrong but it works. For each record found in the dbcontext I convert it so a json string then convert it back to a variable and this gave me a record I could loop through with foreach and refer to each item as item.Value only as what is returned from the json decode is a key/value pair. This has resulted in what I wanted, A db editor / viewer that abstracts the schema away so the code never has to change if the schema does. - and it will.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.