Search code examples
asp.net-mvc-3modelmetadata

How can I get AdditionMetadata attribute from the controller?


In some class, let say Class A, I have a property (of type B):

public class A {
...
[AdditionalMetadata("foo", "bar")]
public B attr {get; set;}
...
}

Later on, in my controller, I instantiate this class and make it a model:

...
A obj = new A();
return View(A);
...

Now, later on, I have a custom editor template (i.e. partial view) for the class B, and from it, I know I can get the AdditionalMetadata by: ViewData.ModelMetadata.AdditionalValues["foo"];

BUT: is there a way to get that metadata property earlier, from the controller?


Solution

  • That should do the job.

    var modelMEtadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(A), "attr");