如何使RequiredAttribute使用枚举字段 [英] How to make RequiredAttribute work with an enum field

查看:163
本文介绍了如何使RequiredAttribute使用枚举字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近意识到RequiredAttribute在枚举字段上不起作用。假设我有两个select元素,称为ddlOfficers和ddlApplicationTypes,这两个表单都使用HtmlHelper方法进行呈现。用于创建ddlOfficers的帮助方法如下:

  @ Html.DropDownListFor(x => x.OfficerID,Model.Officers ,<选择>,新的{id =ddlAddressedOfficer})

Nullable< int>



对于ddlApplicationTypes,我不得不编写一个扩展方法,让我为枚举字段创建dropdownlist :

  @ Html.EnumDropDownListFor(x => x.ApplicationType,new {@class =select-normal}) 

其中ApplicationType类型为自定义枚举,名为AppType

  public Enum AppType {
无= 0,
投诉,
查询,
建议
}
我已经将OfficID和ApplicationType属性与RequiredAttribute进行了装饰。当我没有选择任何ddlOfficers我得到验证警告提交。但是当我没有选择任何ddlApplicationType时,我没有收到任何警告。我可能知道问题的原因:如果我比较两个选择元素,我可以看到,ddlOfficers的第一个选项(Choose)没有指定值,选择时会导致验证进行投诉。但是ddlApplicationType的第一个选项的值为None。所以验证引擎会看到所选的选项有一个值,只是忽略它。你会建议做些什么来让它工作?



编辑:为了使事情更加清晰,看到这里是两个选择元素的html:

 < select class =select-normal input-validation-errordata-val =truedata-val-required =官员id =ddlOfficersname =OfficerID> 
< option value =>& lt;选择& 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 validdata-val =truedata-val-required =选择应用程序类型id =ddlApplicationTypename =ApplicationType>
< option selected =selectedvalue =无>& lt;选择& gt< / option>
< option value =Complaints>投诉< / option>
< option value =Query> Query< / option>
< option value =Suggestion>建议< / option>
< / select>


解决方案

您的自定义帮助器没有任何错误。 HTML清楚地表明,已经添加了所需的数据验证( data-val-required )。简单来说,问题在于,您的枚举始终具有可接受的值。您可能不会考虑可以接受,但从枚举的角度来看,这很好。所以你有两个选择:


  1. 添加自己的自定义验证以确保未被选中。您需要处理这个客户端和服务器端,因为您完全在这里。


  2. 如果您可以更改枚举,您可以删除选项,然后在模型/视图模型属性上使用可空的枚举,即:

      public AppType? ApplicationType {get;组; 

    然后,所需的验证将按预期工作。



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>

解决方案

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.

这篇关于如何使RequiredAttribute使用枚举字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆