My code below:
foreach (var PI in ObjType.GetProperties())
{
var metaData = ModelMetadataProviders.Current.GetMetadataForType(null, PI.GetType());
string DispName = metaData.DisplayName
}
ObjType
is the type of an EF6 schema first entity with DisplayName
been added as a Metadata
class. The error above is probably because PI.GetType()
returns the type of the PropertyInfo
. But I really can't figure out how to get the property itself.
I have look into various example using:
ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
However, in my case, I am not using any Lambda Expression. I just need to construct a list of the properties' DisplayName
and pass it on.
You want PropertyInfo.PropertyType, so change
PI.GetType()
toPI.PropertyType
.