I have a view model that contains an enum
:
public class PasswordChangerIndexViewModel
{
public enum DatabaseTypes
{
Main = 10,
Florida = 20,
Illinois = 30,
Missouri = 40,
NewHampshire = 50,
NewJersey = 60,
Oklahome = 70
};
[DisplayName("Database")]
public DatabaseTypes DatabaseType { get; set; }
}
And in my view, I'm using EnumDropDownListFor
to create a drop down list:
<div class="row">
<div class="col-md-1">
<div class="form-group">
@Html.EnumDropDownListFor(z => z.DatabaseType, "** Select a Database **");
</div>
</div>
</div>
It's working, but I'm wondering if there is a way to change the text. I want New Hampshire
to be rendered instead of NewHampshire
and New Jersey
instead of NewJersey
. Is there a kind of DisplayName attribute or something I can apply to my view model to fix this?
Use the
DisplayAttribute
on your enum members:In general, your should favor using
[Display]
instead of[DisplayName]
since it supports localization.