如何从ASP.NET核心中的模型类动态生成HTML表?
我有一个单价的课程,如下所示:
public partial class UnitPrice
{
public int IdProduct { get; set; }
public int IdUnit { get; set; }
[DisplayName("Service")]
public string Name { get; set; }
[DisplayName("actial Price")]
public decimal Price { get; set; }
[DisplayName("Service Description")]
public string DescriptionProduct { get; set; }
[DisplayName("Version")]
[ReadOnly(true)]
public int Version { get; set; }
[DisplayName("Sap ID")]
public string Material { get; set; }
[DisplayName("Unit")]
public UnitSpecification IdUnitNavigation { get; set; }
}
单位规格如下所示
public partial class UnitSpecification
{
public int IdUnit { get; set; }
public string Name { get; set; }
}
我想转换我在下拉列表中更改版本号时获得的数据。 AJAX:
$('#VersionSelect').change(function () {
console.log('changed');
var version = $('#VersionSelect option:selected').text();
$.ajax({
type: "GET",
url: "./UnitPrices/Version",
data: { version: version },
dataType: 'json',
contentType: 'application/json',
//processData: false,
success: function (response) {
if(response.length === 0)
alert('Some error occured while uploading');
else {
$('#dvData').html(table);
}
},
error: function (e) {
$('#dvData').html(e.responseText);
}
});
});
我已经创建了一个将Datatable转换为HTML表的函数。 当我将我的类转换为数据表时,列名不是它们应该是的,并且外键不正确。 但是当我使用该函数时,列名称不是它们应该是的。
我希望我的表格如下所示:
<html>
<table>
<thead>
<tr>
<th>
Service
</th>
<th>
Actual Price
</th>
<th>
Unit
</th>
<th>
Sap ID
</th>
<th>
Version
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
the service name
</td>
<td>
the actual price
</td>
<td>
the unit name
</td>
<td>
the Sap ID
</td>
<td>
the Version
</td>
</tr>
</tbody>
</table>
</html>
C#代码:
public JsonResult Version(int version)
{
List<UnitPrice> unitprices = new List<UnitPrice>(GetUnitPricesForVersion(version).UnitPrices);
DataTable dt = ToDataTable(unitprices);
//string st = CSVImportController.ConvertDataTableToHTML(dt);
return Json(GetUnitPricesForVersion(version));
}
private UnitPricesViewModel GetUnitPricesForVersion(int version)
{
var list = _context.UnitPrice.Where(x => x.Version == version).Include(u => u.IdUnitNavigation);
return new UnitPricesViewModel
{
UnitPrices = list.ToList(),
Versions = new List<int>()
};
}