I am working on a project where I need to display several hundred properties. To save a lot of tedious layout, I decided to use reflection.
@for (var i = 0; i < Model.SiteAndJobDetails.GetType().GetProperties().Count(); i++)
{
@Model.SiteAndJobDetails.GetType().GetProperties()[i].Name
@Model.SiteAndJobDetails.GetType().GetProperties()[i].GetValue(Model.SiteAndJobDetails, null)
}
Although itβs slower to render, it saves me from writing about two hundred properties and values ββusing HTML helpers. At least that was the plan. However, I need to use @Html.DisplayNameForor something similar to get the attribute value Displayfrom the property.
My thoughts were
@Html.DisplayNameFor(m=>@Model.SiteAndJobDetails.GetType().GetProperties()[i].Name)
But this will not work, I would think, because I use reflection here to get the name of the property. Is there another way?
James source
share