开发者

How does the MetadataType attribute mark a class as a validation class in MVC?

开发者 https://www.devze.com 2023-02-09 19:07 出处:网络
I have my ADO entity generated in MVC 2 and I know that if I want to put custom validation on an object I can do something like this.

I have my ADO entity generated in MVC 2 and I know that if I want to put custom validation on an object I can do something like this.

[MetadataType(typeof(MyEntity_Validation))]
public partial class MyEntity
{
    priv开发者_如何学JAVAate sealed class MyEntity_Validation
    {
        [Required]
        [RegularExpression("[A-Za-z][0-9]{5}")]
        public string SomeField{ get; set; }

    }
}

But I don't know why that works.

How does that work? Some sort of convention?


Metadata is a convention, yes. See http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx. You can add attributes to fields to enforce validation, display, concurrency, all sorts of common usefulness. Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号