ExpressiveAnnotations.Attributes RequiredIf 验证无效

ExpressiveAnnotations.Attributes RequiredIf validation not working

我的以下验证无效,无法解决为什么会这样?

    [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 方法被命中时,它 returns false 抛出的错误消息是 The DepotId field is required.

只需像下面这样将 DepotId 更改为 Nullable,验证就会按预期开始工作:

public int? DepotId{get;set;}

阅读文档后,看起来值类型必须可以为空,然后数据注释才能对其起作用。