本文介绍了Validator.TryValidateProperty不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现Validator.TryValidateProperty,即使是有[必需] DataAnnotation,该TryValidateProperty返回有效响应。

下面是我的客户部分类:

  [MetadataType(typeof运算(Customer.Metadata))]
公共部分类客户:全球:: System.Data.Objects.DataClasses.EntityObject
{
   ...
私人密封类的元数据
    {

        [需要]
        [SSNValidAttribute(的ErrorMessage =核潜艇应该是9数字字符,没有任何标点符号。)
        [DisplayName的(SSN)]
        公共字符串SSN {获得;组; }
...
 

这里是code即返回true:

  ...
VAR的客户=新客户();
            customer.SSN =;
            VAR VC =新ValidationContext(客户,NULL,NULL);
            vc.MemberName =SSN;
            VAR解析度=新的名单,其中,为ValidationResult>();
            VAR的结果= Validator.TryValidateProperty(customer.SSN,VC,水库);
...
 

解决方案

好刚发现的解决方案来处理密封MetadataType类!

  VAR的客户=新客户();
        TypeDescriptor.AddProviderTransparent
        (新AssociatedMetadataTypeTypeDescriptionProvider
            (customer.GetType()),customer.GetType());
        customer.SSN =;
        VAR VC =新ValidationContext(客户,NULL,NULL);
        vc.MemberName =SSN;
        VAR解析度=新的名单,其中,为ValidationResult>();
        VAR的结果= Validator.TryValidateProperty(customer.SSN,VC,水库);
 

我不得不添加以下内容:

  TypeDescriptor.AddProviderTransparent
        (新AssociatedMetadataTypeTypeDescriptionProvider
            (customer.GetType()),customer.GetType());
 

在找到这个地址的解决方案:http://forums.silverlight.net/forums/p/149264/333396.aspx

I am trying to implement Validator.TryValidateProperty and even though there is a [Required] DataAnnotation, the TryValidateProperty returns a valid response.

Here is my Customer partial class:

[MetadataType(typeof(Customer.Metadata))]
public partial class Customer : global::System.Data.Objects.DataClasses.EntityObject
{
   ...
private sealed class Metadata
    {

        [Required]
        [SSNValidAttribute(ErrorMessage = "The SSN should be 9 numeric characters without any punctuation.")]
        [DisplayName("SSN")]
        public String SSN { get; set; }
...

And here is the code that is returning True:

...
var customer = new Customer();
            customer.SSN = "";
            var vc = new ValidationContext(customer, null, null);
            vc.MemberName = "SSN";
            var res = new List<ValidationResult>();
            var result = Validator.TryValidateProperty(customer.SSN, vc, res);
...
解决方案

Ok JUST found the solution for dealing with the sealed MetadataType class!!!

        var customer = new Customer();
        TypeDescriptor.AddProviderTransparent
        (new AssociatedMetadataTypeTypeDescriptionProvider
            (customer.GetType()), customer.GetType());
        customer.SSN = "";
        var vc = new ValidationContext(customer, null, null);
        vc.MemberName = "SSN";
        var res = new List<ValidationResult>();
        var result = Validator.TryValidateProperty(customer.SSN, vc, res);

I had to add the following:

        TypeDescriptor.AddProviderTransparent
        (new AssociatedMetadataTypeTypeDescriptionProvider
            (customer.GetType()), customer.GetType());

Found solution at this address:http://forums.silverlight.net/forums/p/149264/333396.aspx

这篇关于Validator.TryValidateProperty不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 18:11