MetadataType problem

EDIT: I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx

Before validating, you need to manually register the metadata class:

TypeDescriptor.AddProviderTransparent(
            new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Person), typeof(PersonMetadata)), typeof(Person));

        List<ValidationResult> res = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), res, true);

(Original answer follows)

The problem isn’t specifically with your partial class, it’s that Validator.TryValidateObject doesn’t seem to recognize the MetaDataType attribute. I have the same problem – the built-in validation in MVC 2 recognizes the metadata class, but TryValidateObject doesn’t.

See these:
Validating DataAnnotations with Validator class
Validation does not work when I use Validator.TryValidateObject

As a side note, I don’t know if it’s necessary, but all examples I’ve seen for metadata classes employ the default get/set on each property:

[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name { get; set; }

Leave a Comment