簡體   English   中英

企業庫驗證應用程序塊。 如何驗證模型類?

[英]Enterprise Library Validation Application Block. How to validate the model class?

我在我的項目中使用Enterprise Library 5.0-Validation Application Block 5.0.505.0 我已經使用驗證注釋(例如NotNullValidator)修改了Model類屬性。 但是,當我運行項目並用數據填充特定的Model類時,它不會自動驗證Model。 我需要按以下方式手動測試模型嗎?

USAddress testaddress = new USAddress(); //this is the Model instance which I am validating

        //Create a new validator using the ValidationFactory method
        Validator validator = ValidationFactory.CreateValidator<USAddress>();
        ValidationResults results = new ValidationResults();
        validator.Validate(testaddress, results);

我是否每次都要驗證Model類? 當我將數據填充到此類的屬性時,它不會自動得到驗證嗎?

模型驗證應使用:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

您需要引用以下內容:

System.ComponentModel.DataAnnotations

然后,您將可以訪問以下屬性:

[Required]
[StringLength]
[RegularExpression]
[Compare]

[必需]與[NotNullValidator]相似。

HTH!

暫無
暫無

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

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