I used Database-First to generate a model class (EDMX file), and I want to validate using “MetadataType”. I read solutions here but they didn’t work for me.
Here is my code:
[MetadataType(typeof(MovieEntitiesMetaData))]
public partial class MovieEntities
{
}
public class MovieEntitiesMetaData
{
[DisplayFormat(DataFormatString = "{0:c}")]
public Nullable<global::System.Decimal> PRICE { get; set; }
}
Is there anything missing here, or why did my solution did not work?
Create a new file called
MoviePartial.csand place the following code inside it:You also need to pass the
Movietype to the view so that the data annotations can be wired up. If you have a custom view model the data annotations won’t get in action.In the Create/Edit view you must have:
In the Details view you must have:
For more on this, just follow this nice step by step tutorial:
Validation with the Data Annotation Validators (C#)