Enum DisplayFor

May 26, 2015 at 3:22 PM
Edited May 26, 2015 at 3:28 PM
Hi All,

I'm wondering if anyone has been able to successfully get the Grid.MVC to use the "Displayfor" attribute on an Enum property. My model looks like this:
    public enum BackupType
    {
        [Display(Name = "Windows Server")]
        WindowsServer,
        [Display(Name = "Symantec Backup Exec")]
        BackupExec,
        [Display(Name = "Backup Assist")]
        BackupAssist,
        [Display(Name = "Remote Agent")]
        RemoteAgent,
        [Display(Name = "Cloudberry")]
        Cloudberry,
        [Display(Name = "SQLBak")]
        SQLBak,
        [Display(Name = "None")]
        None
    }
This works fine in all other parts of my application in conjunction with a display template where I get the display value:
@model Enum

@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
    // Display Enum using same names (from [Display] attributes) as in editors
    string displayName = null;
    foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model))
    {
        if (item.Selected)
        {
            displayName = item.Text ?? item.Value;
        }
    }

    // Handle the unexpected case that nothing is selected
    if (String.IsNullOrEmpty(displayName))
    {
        if (Model == null)
        {
            displayName = String.Empty;
        }
        else
        {
            displayName = Model.ToString();
        }
    }

    @Html.DisplayTextFor(model => displayName)
}
else
{
    // This Enum type is not supported.  Fall back to the text.
    @Html.DisplayTextFor(model => model)
}
This does not work though within the grid. Does anyone have any suggestions on how to get this working? Essentially all I want to do is honour the DisplayFor Data Annotations in the model for an Enum.