5

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?

aaron
  • 718
  • 2
  • 10
  • 19
  • I wonder if you can create a new class that inherits from the one in the library, then override the appropriate properties and add annotations in your version. If the library utilizes this class internally a bunch, this might not be possible, even if it works – Prescott Dec 06 '11 at 02:41

2 Answers2

3

Create your own ModelMetadataProvider and this should do want you want

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

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
2

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

Community
  • 1
  • 1
Turbot
  • 5,095
  • 1
  • 22
  • 30