Question

I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't use data annotation attributes since some of the metadata could change based on configured values, so I need a solution that will let me set the metadata on every request. I've been reading about custom model metadata providers, but I can't find anything about support for dynamic metadata scenarios.

Does the MVC framework cache the metadata from the model metadata provider, or will the CreateMetadata method be called on every request? Is a custom model metadata provider a good solution for dynamic metadata, or should I just put it in the view model?

Edit: This isn't necessarily validation metadata I'm talking about, so I'm not looking for a validation specific solution. It could just be metadata to be displayed or used by the view (in HTML 5 data attributes for example).

Another example might be a database driven field label. Usually one would use the Name property of the Display attribute, but this wouldn't work if you wanted to dynamically set the display name based on a value in the database.

Était-ce utile?

La solution

You can derive your custom provider from DataAnnotationsModelMetadataProvider so you will inherit all the base behaviour.

Yes, CreateMetadata is called on every request, so you can do something fancy, like go to DB and load some data that may change in time.

For labels I would implement custom ResourceProvider that uses Db as the source of label (based on current culture / account, etc.) Example is here: http://www.codeproject.com/Articles/14190/ASP-NET-2-0-Custom-SQL-Server-ResourceProvider

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top