-
Notifications
You must be signed in to change notification settings - Fork 2.1k
DisplayFor doesn't retrieve enum values #2430
Comments
To solve the problem Temporarily。add a shared view of enum。 @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)
} |
👍 |
It is not working for RC1, cannot find |
Same here, "EnumHelper doesn't exist in current context." |
|
I tried to replace |
Sorry, I glazed over your For this case, you likely need to var value = item.Value ?? item.Text;
var selected = currentValues.Contains(value); |
Ok, I got it working now 😄 I used the following code : @model Enum
@inject Microsoft.AspNet.Mvc.ViewFeatures.IHtmlGenerator htmlGenerator
@{
// Display Enum using same names(from [Display] attributes) as in editors
string displayName = null;
var currentValues = htmlGenerator.GetCurrentValues(ViewContext, ViewData.ModelExplorer, Model.GetType().Name, false);
foreach (SelectListItem item in Html.GetEnumSelectList(Model.GetType()))
{
if (currentValues.Contains(item.Value))
{
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)
}``` |
Thank you Pierre, I have lost many hours to find your answer. Microsoft could put this option by default in Html.DisplayFor(). |
@Eilon Is this really an enhancement? Is this just a bug that should be fixed? |
@weitzhandler this should be fixed in 830983a. Thanks for the report! |
@ryanbrandenburg Will this also be added to ASP.NET MVC non-core? What is the policy regarding fixes and features in ASP.NET MVC 5? Is it documented somewhere? |
@fschmied we don't generally back-port issues. Will this be an issue for you in ASP.NET MVC 5? |
@Eilon We can use the workaround (a dedicated display template for Enum) in MVC 5; I just wondered what the policy for backporting was - does MVC 5 still get features and/or fixes for bugs reported here on GitHub? Is the policy documented somewhere? |
@fschmied we are still working on MVC 5.x, but usually only to fix high-priority bugs and ensuring that it all still works. By far most of our efforts are on ASP.NET Core. |
The enum support is indeed a great add, thanks for implementing it.
Anyway, I've found something missing about it, when calling
DisplayFor(model => model.EnumField)
doesn't render the enum value, it won't retrieve the value from resources, but it will render the original value:The above works just fine with
@Html.EnumDropDownListFor(model => model.MaritalStatus)
, but doesn't work forDisplayFor
.Please fix this.
The text was updated successfully, but these errors were encountered: