(332) 331-8677

How to Get the Value of DisplayName Attribute

@model Models.MyModel
@using System.ComponentModel.DataAnnotations

@{
    var properties = Model.GetType().GetProperties();
}

<table>
    @foreach (var item in properties)
    {
        @if (item.GetValue(Model) != null && item.Name != "Text" && item.Name != "Img" && item.Name != "Id"
            && item.Name != "BrowserTitle" && item.Name != "Description" && item.Name != "Title" && item.Name != "Sort")
        {
            <tr>
                <td>
                    @{
                        bool isDef = Attribute.IsDefined(item, typeof(DisplayAttribute));
                        DisplayAttribute attr = (DisplayAttribute)Attribute.GetCustomAttribute(item, typeof(DisplayAttribute));
                    }
                    @if (isDef)
                    {
                        @attr.Name
                    }
                </td>
                <td>@item.GetValue(Model)</td>
            </tr>
        }
    }
</table>