I am working on a project where I need to output a few hundred properties on to the screen. To save myself lots of tedious markup, I decided to use reflection.
//markup removed to keep this concise
@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 slower to render, this will save me writing out about 2 hundred properties and values with HTML helpers. At least, that was the plan. However, I need to use @Html.DisplayNameFor
or something similar to pick up the Display
attribute value from the property.
My intial thoughts were
@Html.DisplayNameFor(m=>@Model.SiteAndJobDetails.GetType().GetProperties()[i].Name)
But that does not work, I would imagine because I am using reflection here to get the property name. Is there another way?
You can get it using the Metadata (that's what the framework does anyway):
@Andrei was correct to use the
ViewData.ModelMetadata
but had the syntax slightly off. The correct syntax isThe final solution is to check the property exists, if it does use it, otherwise use the property name
I prefer this answer: https://stackoverflow.com/a/31448493/9266796
Updated to today's syntax:
Or this one's even easier actually: https://stackoverflow.com/a/19940277/9266796
Alternatively, add this to your razor:
Then you can:
I like using this when writing JavaScript that receives JSON. It lets you do this:
This way you get autocomplete inside the lambda and you are free to rename the property without worrying about breaking your front end code.