I am writing a custom ModelMetadataProvider which extends default DataAnnotationsModelMetadataProvider. Unfortunately i came across an issue:
ModelMetadata.Container property is always null. According to msdn description of ModelMetadata Class, container property should not be null when model represents a property:

But when overriding GetMetadataForProperty method, after calling base implementation Container property always returns as null:

protected override ModelMetadata GetMetadataForProperty(
Func<object> modelAccessor,
Type containerType,
PropertyDescriptor propertyDescriptor)
{
ModelMetadata metadata = base.GetMetadataForProperty(modelAccessor, containerType, propertyDescriptor);
// metadata.Container always null here
}
On the other way, when accessing ModelMetadata from ViewData, Container property gets filled as expected:

Thus I have couple of questions related to this issue:
Is it an expected behavior that ModelMetadata.Container returns empty from
GetMetadataForPropertyor it is a bug?Is there a way to get
ModelMetadata.Containerproperty filled inside ModelMetadataProvider?When ModelMetadata.Container property actually gets it's content?
Metadata is created from the innermost properties out, so the container model's metadata has not yet been created at the time
GetMetadataForPropertyis called (the same applies forOnMetadataCreatedwhen you implementIMetadatAware). The reason you can acess it fromViewDatais that the process of creating all metadata has been completed.