How to make RequiredAttribute work with an enum field

女生的网名这么多〃 提交于 2020-01-03 17:50:09

问题


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="">&lt;Choose&gt;</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">&lt;Choose&gt;</option>
   <option value="Complaint">Complaint</option>
   <option value="Query">Query</option>
   <option value="Suggestion">Suggestion</option>
</select>

回答1:


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:

  1. 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.

  2. 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.




回答2:


[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



来源:https://stackoverflow.com/questions/25548796/how-to-make-requiredattribute-work-with-an-enum-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!