I have a "generic" Razor view for displaying table for a model which is a collection like IEnumerable<IFirstLevetOverviewModel>
. On this view, I'm looping for all items in the collection and displaying columns and header via reflection. So to build header I'm using this code:
Model property:
[Display(Name = "Personalkategorie_OffiziellerStellenplan"]
public string PersonalNumber { get; set; }
View:
@foreach (var prop in Model.FirstOrDefault().GetType().GetProperties())
{
if (!prop.PropertyType.Name.Contains("IEnumerable"))
{
<th style="width: calc(100%/@propsCount);">
@DisplayNameReflected(prop);
</th>
}
}
Helper:
@helper DisplayNameReflected(PropertyInfo property)
{
if (!property.PropertyType.Name.Contains("IEnumerable"))
{
var dd = property.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
if (dd != null)
{
var name = dd.Name;
}
}
}
And it works fine. But now I have a requirement to implement localization, so I need to use ResourceType ([Display(Name = "Personalkategorie_OffiziellerStellenplan", ResourceType = typeof(Labels))]
). But I can't figure out how to improve my helper to get localized name. Is it possible at all? Or maybe there is some cleaner way to do it?
Aucun commentaire:
Enregistrer un commentaire