Esempio n. 1
0
        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);
        }
 public AgentValidatorFacts()
 {
     _agentValidator = new AgentValidator();
 }
Esempio n. 3
0
 public AgentValidatorFacts()
 {
     _agentValidator = new AgentValidator();
 }
 public AgentValidatorTests()
 {
     _validator = new AgentValidator();
 }