I know there are already other threads about this. I've been reading them. Here's what I've got:
namespace Books.Entities
{
public enum Genre
{
[Display(Name = "Non Fiction")]
NonFiction,
Romance,
Action,
[Display(Name = "Science Fiction")]
ScienceFiction
}
}
Model:
namespace Books.Entities
{
public class Book
{
public int ID { get; set; }
[Required]
[StringLength(255)]
public string Title { get; set; }
public Genre Category { get; set; }
}
}
Then, in a view:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
</tr>
I would think the framework would use the DisplayName property automatically. Seems weird that it doesn't. But, whatever. Trying to overcome that with an extension (found this in another thread on the same question)...
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.GetName();
}
}
Looks like it should work, but when I try and use it:
@Html.DisplayFor(modelItem => item.Category.GetDispayName())
I get this error:
{"Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."}