public void GiveACommunicationWithAnUnknownType_Validate_ShouldHaveAValidationError()
        {
            // Arrange.
            var Agent = new Agent
            {
                Name           = "a",
                Communications = new List <Communication>
                {
                    new Communication
                    {
                        CommunicationType = CommunicationType.Unknown,
                        Details           = "a"
                    }
                }
            };

            // Act.
            _validator.ShouldHaveChildValidator(agent => agent.Communications, typeof(CommunicationValidator));
            var result = _validator.Validate(Agent);

            //_validator.ShouldNotHaveValidationErrorFor(agent => agent.Communications, new[] {communication});

            // Assert.
            result.Errors.ShouldContain(x => x.PropertyName == "Communications[0].CommunicationType");
        }
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(Agent))
            {
                return(false);
            }
            var modelName           = bindingContext.ModelName;
            var valueProviderResult =
                bindingContext.ValueProvider.GetValue(modelName);

            if (valueProviderResult == null)
            {
                return(false);
            }
            bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);
            var val = valueProviderResult.RawValue as string;

            try
            {
                var agent     = new Agent(val as string);
                var validator = new AgentValidator();

                //fluent validation
                var results = validator.Validate(agent);
                if (!results.IsValid)
                {
                    results.AddToModelState(bindingContext.ModelState, null);
                    return(false);
                }

                bindingContext.Model = agent;
                return(true);
            }
            catch (System.Exception ex)
            {
                bindingContext.ModelState.AddModelError(modelName, ex);
                return(false);
            }

            return(false);
        }