How to get values dynamically from a collection in a model in Razor Pages?

Viewed 12

I'm trying to render lists of elements dynamically in ASPNETCore Razor Pages but I can't find the way to show the values of properties using Display Helper. M

<table class="table datatable">
    <thead>
        <tr>
        @foreach (var item in Model.GetFieldNamesForList())
        {
            <th>
                @Html.DisplayName(item)
            </th>
        }
            <th></th>
        </tr>
    </thead>
    <tbody>
     @foreach (var item in Model.School) {
        <tr>
        @foreach (var propertyName in Model.GetFieldNamesForList())
        {
            <td>
                // How to use @Html.Display using item from Model.School and the propertyName
            </td>
        }

            <td>
                <a asp-page="./Edit" asp-route-id="@item.Id" class="btn btn-primary btn-sm"><i class="material-icons md-16">edit</i></a>
                <a asp-page="./Details" asp-route-id="@item.Id" class="btn btn-primary btn-sm"><i class="material-icons md-16">visibility</i></a>
                <button action="submit" asp-page-handler="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-sm"><i class="material-icons md-16">delete</i></button>
            </td>
        </tr>
    }
    </tbody>
</table>

In the meantime, I use reflection directly, but I would prefer use the Display method from helpers.

@item.GetType().GetProperty(propertyName).GetValue(item, null)
0 Answers
Related