dimanche 19 avril 2015

ASP.NET MVC 5.1 EnumHelper.EnumDropDownListFor on enum with Flags attribute

I want to make a dropdownlist of an enum property on my ViewModel.


I've been searching for examples like this and this where they use enums for displaying a select and radiobuttons controls if a form.


I have an enum like this



class MyViewModel
{
public JobCategory JobCategory {get; set;}
}

[Flags]
public enum JobCategory
{
/// <summary>
/// Ninguna
/// </summary>
[Display(Name = "N/A")]
None = 0,

/// <summary>
/// Diseño Grafico
/// </summary>
[Display(Name = "Diseño Gráfico")]
GraphicDesign = 1

...
}


And the form



@model MyViewModel

<div class="col-xs-3">
@(EnumHelper.IsValidForEnumHelper(Model.JobCategory.GetType())
? Html.EnumDropDownListFor(d => d.JobCategory, new { @class = "form-control" })
: Html.EditorFor(d => d.JobCategory, new { @class = "form-control" }))
</div>


When I remove the Flags attribute the method EnumHelper.IsValidForEnumHelper(Model.JobCategory.GetType()) returns true and displays a select otherwise returns false and the form displays a textbox.


Someone know how can I use this helper without removing the Flags attribute?


Aucun commentaire:

Enregistrer un commentaire