Validator.TryValidateProperty 不起作用

发布于 2024-11-15 10:51:32 字数 890 浏览 2 评论 0原文

我正在尝试实现 Validator.TryValidateProperty,即使有 [Required] DataAnnotation,TryValidateProperty 也会返回有效响应。

这是我的 Customer 部分类:

[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; }
...

这是返回 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);
...

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);
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-11-22 10:51:32

好的,刚刚找到了处理密封 MetadataType 类的解决方案。

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);

我必须添加以下内容:

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

在此地址找到解决方案:
http://forums.silverlight.net/forums/p/149264/333396。 ASPX

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文