Mam następujący obiekt (domenę) obiekt i model, który zawiera wyliczenie. Nazwa wyświetlana jest poprawnie wyświetlana i działa dla EnumDropdownList, ale z jakiegoś powodu nie dla helpera DisplayFor, wszystko, co jest pokazane, jest rzeczywistą nazwą wyliczeniową.MVC 5.1 Razor DisplayNie działa z Enum DisplayName
Nie jestem pewien, czego mi brakuje, asp.net MVC 5.1 dodał obsługę nazwy wyświetlanej, więc nie powinienem tworzyć własnych metod pomocniczych. Zobacz: https://aspnet.codeplex.com/SourceControl/latest#Samples/MVC/EnumSample/EnumSample/Models/Enums.cs
public class Addon
{
public int Id { get; set; }
public AddonType AddonType { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public bool IsActive { get; set; }
}
public enum AddonType : byte
{
[Display(Name = "Cake Theme")]
CakeTheme,
[Display(Name = "Cake Flavour")]
CakeFlavour,
[Display(Name = "Cupcake Icing")]
CupcakeIcing,
[Display(Name = "Party Addon")]
AddOn
}
MODEL
public class AddonModel
{
public int Id { get; set; }
public AddonType AddonType { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public bool IsActive { get; set; }
}
WIDOK
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Type</th>
<th>Name</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(model => item.AddonType)
</td>
<td>
@Html.DisplayFor(model => item.Name)
</td>
<td>
@Html.DisplayFor(model => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>