While trying to get DisplayFor(enum) to show the DisplayName of the Enum instead of the Enum Text I came across this question that pointed me in the right direction:
MVC 5.1 Razor DisplayFor not working with Enum DisplayName
Using that I created a helper to show the DisplayName instead.
Model:
Public Enum QnStatus
<Display(Name:="Öppen")> Open
<Display(Name:="Avslutad")> Closed
<Display(Name:="Väntar på svar från kund")> WaitingOnCustomer
<Display(Name:="Väntar på svar från leverantör")> WaitingOnSupplier
End Enum
Helper:
@Model Enum
@If EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata) Then
Dim displayname As String = ""
For Each item As SelectListItem In EnumHelper.GetSelectList(ViewData.ModelMetadata, DirectCast(Model, [Enum]))
If item.Selected Then
If Not IsNothing(item.Text) Then
displayname = item.Text
Else
displayname = item.Value
End If
End If
Next
If String.IsNullOrEmpty(displayname) Then
If Model Is Nothing Then
displayname = String.Empty
Else
displayname = Model.ToString()
End If
End If
@Html.DisplayFor(Function(model) displayname)
Else
@Html.DisplayTextFor(Function(model) model)
End If
The problem I am having is that now when using DisplayFor in a view it returns the following (example for WaitingOnCustomer):
WaitingOnCustomer Enum Väntar på svar från kund
So I get Enum Text + Enum Type + DisplayName.
Without using the helper I get:
WaitingOnCustomer
Why is it returning the Enum Text and Type when using the helper?