Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Data DataAnnotations setting dynamically using t4 template

Is there any way to set dynamically the dataannotations according to some values in database?

My requirement is like this:

Display(Name="DispName")]

public string DName{get;set;}

Instead of hardcoding Name="DispName" for Display DataAnnotation, I want to fetch some value from DataBase and fit int the Name attribute. like:

[Display(Name=SomeValueFromDB)]

public string DName{get;set;}

This should be done using t4 template also.

Is there any way to accomplish this?

Regards,

Sujith

like image 213
Sujith S Nair Avatar asked May 16 '13 11:05

Sujith S Nair


1 Answers

Not sure if i get what you mean. To me T4 generation doesnt make much sense in your case but have a look at this Phil Haack approach: http://haacked.com/archive/2011/07/14/model-metadata-and-validation-localization-using-conventions.aspx

In contrast to your request he is modifying data annotations at runtime, using custom ModelMetadataProvider.

like image 168
Boris Bucha Avatar answered Sep 21 '22 19:09

Boris Bucha