Tuesday, September 5, 2017

Asp.net MVC MetaDataType attribute

Introduction

Developers working on asp.net mvc with Entity framework, POCO model or swagger generated web api code generation, mvc metadatatype attribute comes very handy. Everyone should be aware of this construct and usage. I want to file this for my reference hence added small reference here.

Asp.net MVC MetaDataType attribute

Specifications:
  • Only applied to partial class
  • Can be used when you have to do additional validation on entity framework generated entity class or Swagger generated api code for client.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
//Class generated from Entity Framework Database First
public partial class YourModelClass
{
    public string YourProperty{get;set;}
}

//Your Partial Class
[MetadataType(typeof(YourModelClassMetaData))]
public partial class YourModelClass
{
}

//The class that add your attributes
public class YourModelClassMetaData
{
    //validation
    [Required]
    public object YourProperty{get;set;};
}

From <http://patrickdesjardins.com/blog/why-it-is-wrong-to-use-the-asp-net-mvc-metadatatype-attribute

No comments :