add data annotations programmatically

点点圈 提交于 2019-12-10 15:11:21

问题


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?


回答1:


Create your own ModelMetadataProvider and this should do want you want

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




回答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



来源:https://stackoverflow.com/questions/8394564/add-data-annotations-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!