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 both rendered with the help of HtmlHelper methods. The helper method for creting ddlOfficers is as follows:
@Html.DropDownListFor(x => x.OfficerID, Model.Officers, "<Choose>", new { id = "ddlAddressedOfficer" })
Where OfficerID is a Nullable<int>
For ddlApplicationTypes I had to write an extension method that would let me create dropdownlist for enum fields:
@Html.EnumDropDownListFor(x => x.ApplicationType, new { @class = "select-normal" })
Where ApplicationType is of type custom enum called AppType
public Enum AppType{
None=0,
Complaint,
Query,
Suggestion
}
I've decorated both OfficerID and ApplicationType properties with RequiredAttribute. When I don't select anything on ddlOfficers I get validation warning on submitting. But I don't get any warning when I don't select anything on ddlApplicationType. And I probably know the cause of the problem:If I compare the two select elements I can see that the first option (Choose) of ddlOfficers has no value specified, which when selected causes the validation to complain. But the first option of ddlApplicationType has value of "None". So the validation engine sees that the selected option has a value and simply ignores it. What would you suggest to do to get it working?
EDIT: To make things more clear to see here is the html for both select elements:
<select class="select-normal input-validation-error" data-val="true" data-val-required="Choose the addressed officer" id="ddlOfficers" name="OfficerID">
<option value=""><Choose></option>
<option value="1">Ben Martin</option>
<option value="2">Nick Carter</option>
<option value="3">Sebastian Van</option>
</select>
<select class="select-normal valid" data-val="true" data-val-required="Select the application type" id="ddlApplicationType" name="ApplicationType">
<option selected="selected" value="None"><Choose></option>
<option value="Complaint">Complaint</option>
<option value="Query">Query</option>
<option value="Suggestion">Suggestion</option>
</select>
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.
public enum ReorderLevel
{
Zero = 0,
Five = 5,
Ten = 10,
Fifteen = 15,
Twenty = 20,
TwentyFive = 25,
Thirty = 30
}
[MetadataType(typeof(ProductMD))]
public partial class Product
{
public class ProductMD
{
[EnumDataType(typeof(ReorderLevel))]
public object ReorderLevel { get; set; }
}
}
EnumDataTypeAttribute MSDN Article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With