{"action":"opened","issue":{"url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/135","labels_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/135/labels{/name}","comments_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/135/comments","events_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/135/events","html_url":"https://github.com/JeremySkinner/FluentValidation/issues/135","id":110019328,"number":135,"title":"ContainerType doesn't correspond to the type of Container","user":{"login":"Andree643","id":6212595,"avatar_url":"https://avatars.githubusercontent.com/u/6212595?v=3","gravatar_id":"","url":"https://api.github.com/users/Andree643","html_url":"https://github.com/Andree643","followers_url":"https://api.github.com/users/Andree643/followers","following_url":"https://api.github.com/users/Andree643/following{/other_user}","gists_url":"https://api.github.com/users/Andree643/gists{/gist_id}","starred_url":"https://api.github.com/users/Andree643/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Andree643/subscriptions","organizations_url":"https://api.github.com/users/Andree643/orgs","repos_url":"https://api.github.com/users/Andree643/repos","events_url":"https://api.github.com/users/Andree643/events{/privacy}","received_events_url":"https://api.github.com/users/Andree643/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-06T14:27:44Z","updated_at":"2015-10-06T14:27:44Z","closed_at":null,"body":"I'm using viewmodel consisting of other models in my view. That means the model isn't flat. This is OK everywhere but in the validation. The model looks like this:\r\n```C#\r\n// ViewModel\r\npublic class PeriodDetailModel\r\n{\r\n    public PeriodModel Period { get; set; }\r\n    // Other properties\r\n}\r\n\r\n// Model\r\npublic class PeriodModel\r\n{\r\n    public int Id { get; set; }\r\n    // Other properties\r\n}\r\n```\r\n\r\nThe validator is binded using IoC/DI. The problem is the lambda expression x => x.Period.Id cause the MVC generates metadata with `Container` as instance of `PeriodDetailModel`, but the `ContainerType` is type of `PeriodModel`, which cause Castle looking for validator binded to `PeriodModel` whereas it should be looking for validator binded to `PeriodDetailModel`.\r\n![metadatacontainertypeissue](https://cloud.githubusercontent.com/assets/6212595/10311219/223e3be8-6c46-11e5-9a01-de7682bbbbd7.png)\r\n\r\nEasy fix for MVC5 is to replace in class `FluentValidationModelValidatorProvider` and method\r\n`IValidator CreateValidator(ModelMetadata metadata, ControllerContext context)` line\r\n```C#\r\nreturn ValidatorFactory.GetValidator(metadata.ContainerType);\r\n```\r\nwith\r\n```C#\r\nreturn ValidatorFactory.GetValidator(metadata.Container.GetType());\r\n```\r\n\r\nBut this can't be used in MVC4 as it doesn't have property `Container`."}}