簡體   English   中英

ExpressiveAnnotations.Attributes必需如果驗證不起作用

[英]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方法時,它返回false,並且拋出的錯誤消息是The DepotId field is required.

只需將DepotId更改為Nullable ,如下所示,驗證就開始按預期進行:

public int? DepotId{get;set;}

閱讀文檔后,看起來值類型必須為空,然后數據注釋才能對其使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM