I\'ve recently realized that RequiredAttribute does not work on enum fields. Let\'s say I have two select elements called ddlOfficers and ddlApplicationTypes on the form bot
There's nothing wrong with your custom helper. The HTML clearly shows that the required data validation has been added (data-val-required
). Simply, the issue is that that your enum always has an acceptable value. You may not consider None
acceptable, but from the enum's perspective, it's just fine. So you have two choices:
Add your own custom validation to ensure that None
is not selected. You'll need to handle this both client and server-side, because you're completely on your own here.
If you can change the enum, you can remove the None
option, and then simply use a nullable enum on your model/view model property, i.e.:
public AppType? ApplicationType { get; set; }
Then, the required validation will work as expected.
[EnumDataType(typeof(AppType))]
This class lets you map the underlying value in a column to a corresponding enumeration constant name. This lets you define an enumeration that contains descriptive values that correspond to database values, and then use the enumeration constant names instead of the database values when data is displayed.
MSDN Article