我的以下验证无效,并且无法解决为什么会出现这种情况?
[Display(Name = "Bus")]
[RequiredIf("SelectedWidgetText == 'Referral'", ErrorMessage = "Please select a Vehicle.")]
public int DepotId{ get; set; }
public string SelectedWidgetText { get; set; }
我也尝试了以下方法:
[Display(Name = "Bus")]
[AssertThat("SelectedWidgetText == 'Referral'", ErrorMessage = "Please select a Vehicle.")]
public int DepotId{ get; set; }
在服务器端,当按下modelstate.isvalid方法时,它返回false,并且抛出的错误消息为The DepotId field is required.
答案 0 :(得分:0)
只需将DepotId
更改为Nullable
,如下所示,验证就开始按预期进行:
public int? DepotId{get;set;}
在阅读了文档之后,看起来值类型必须为空,然后数据注释才能在它们上起作用。