I need to display all fields of dto-list in razorpage.I have about 70 fields of each dto object.
now i have such code:
@foreach (var ft in Model.DtoForeignClients)
{
<tr>
<td>@ft.Name</td>
<td>@ft.Head</td>
<td>@ft.Customer</td>
<td>@ft.Item</td>
//~66 another fields
</tr>
it works, but i looking for improve my code. i want to to something :
@foreach (var ft in Model.DtoForeignClients)
{
<tr>
@for (int i = 0; i<ft.CountOfFields;i++)
<td>@ft[i]</td>
</tr>
because i do not want to repeat about 70 times.
How can i do this? Should i looking into IEnumerable realization for my DTO? or any another ideas, thank you
Use reflection in your view to get the FieldInfo[] of the object. Replace DtoForeignClient
with your real type of ft
.
@using System.Reflection;
@{
var fields = typeof(DtoForeignClient).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
}
@foreach (var ft in Model.DtoForeignClients)
{
<tr>
@foreach(FieldInfo f in fields)
{
<td>@f.GetValue(ft)</td>
}
<tr>
}