aaron
aaron

Reputation: 748

add data annotations programmatically

I am working on an asp mvc3 app that is using .NET 4.

However the model I want to use is in a class library that is using .NET 3.5 and cannot change.

Can I add the .NET 4 data annotations to some properties in my model in the controller programatically to get some easy validation?

Upvotes: 5

Views: 11129

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30162

Create your own ModelMetadataProvider and this should do want you want

http://www.dotnetcurry.com/ShowArticle.aspx?ID=715

Upvotes: 3

Turbot
Turbot

Reputation: 5233

Interesting, I never try that before but could you declare a partial class in your .NET 4 project and use MetadataTypeAttribute to create a sibling class that hold the metadata and decorate it with validation.

Never try that before but similar approach is used in ASP.NET MVC (only for Silverlight). you could see Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

Upvotes: 2

Related Questions