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?
FlagsAttribute
and returnsfalse
if it is set. You would also have a problem with a drop down (I suppose multi-select), as theNone
value would always be selected.EnumDropDownListFor
does not support Flags enums. However, I think this is an excellent opportunity to create a custom helper. I think that a multi-select dropdown could handle this scenario quite nicely. How did you end up solving your problem?