dimanche 10 mai 2015

MVC5 dropdown for enums using metadata

I am trying to have my MVC5 web application display values listed on an enum. I have two problems, and I guess they are related: First problem is that I cannot put a display name for my enum members:

This is my class Moneda.vs :

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.ComponentModel

Public Class Moneda
    Public Enum Moneda
        <Display(Name = "Soles")> _   >> gives error *
        PEN
        EUR
        USD
    End Enum
End Class

*Error is: 'Name' is not declared. File I/O functionality is available in Microsoft.Visualbasic namespace. Even if I do import this namespace error persists.

Secondly, my views are not displaying the dropdown. As per (http://ift.tt/1pii8R3) I created two files (I will just show the editor one, because until I do not solve problem 1, there is no use with the DisplayTemplates one):

../Views/Shared/EditorTemplates/Enum.vbhtml as following:

@Model Enum
@If (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata)) Then
    @Html.EnumDropDownListFor(Function(model) model, New With {.htmlAttributes = New With {.class = "form-control"}})
Else
    @Html.TextBoxFor(Function(model) model, New With {.htmlAttributes = New With {.class = "form-control"}})
End If

Now, here's a thing that might have a clue, is just that I am not seeing it: I am using a metadata file to format the way the fields are displayed on my view. In the case of the specific entity that uses this enum, metadata is as follows:

<MetadataType(GetType(SueldosMetadata))> _
Partial Public Class Sueldos
End Class
Public Class SueldosMetadata
   <DisplayName("Fecha de incorporación")> _
   <DataType(DataType.Date)> _
   <DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0:yyyy-MM-dd}")> _
    Public Property Desde


    'THIS IS THE FIELD WHICH SHOULD CALL THE ENUM
    <DisplayName("Moneda (Código ISO)")> _
    <StringLength(3)> _
    Public Property Moneda As Moneda        'here indeed I am tying it

End Class

For this metadata thing, the rest of tasks are working ok.

So, my two questions are: -Why I cannot use _ on my enum members -Why is the dropdown not showing on my views?

Thanks, and I do apologise for the format of this post, it is my first one.

Aucun commentaire:

Enregistrer un commentaire