开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜