2014-06-20 10 views
27

Ho il seguente oggetto (dominio) oggetto e modello che contiene un enum. Il nome visualizzato viene visualizzato correttamente e funziona per un EnumDropdownList ma per qualche motivo non per l'helper DisplayFor, tutto ciò che viene mostrato è il nome enum effettivo.Visualizzazione del rasoio MVC 5.1 Per non funzionare con Enum DisplayName

Non sono sicuro di ciò che mi manca, asp.net MVC 5.1 ha aggiunto il supporto del nome visualizzato per questo, quindi non avrei dovuto creare i miei metodi di supporto. Vedere: 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 
} 

MODELLO

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; } 
} 

VISTA

<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> 

risposta

62

Crea nuova cartella Vista/shared/DisplayTemplates
Aggiungi Vista parziale vuoto denominato Enum, nella cartella
Sostituire Enum Visualizza il codice con:

@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) 
} 

Ecco lo link to detailed article by Shahriar Hossain

Problemi correlati