2012-11-20 46 views
1

我在我的项目中使用Enterprise Library 5.0 - Validation Application Block 5.0.505.0。我使用验证注释修改了我的Model类属性(如NotNullValidator)。但是当我运行我的项目并用数据填充特定的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类?当我将数据填充到此类的属性时,它不会自动验证吗?

回答

1

验证配车型应该使用:

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

你需要作出引用:

System.ComponentModel.DataAnnotations 

然后,您将有机会获得类似的属性:

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

[必填]类似于[NotNullValidator]。

HTH!

相关问题