public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (!CanBindType(bindingContext.ModelType))
            {
                return false;
            }
            if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
            {
                return false;
            }

            bindingContext.Model = Activator.CreateInstance(bindingContext.ModelType);
            ComplexModelDto dto = new ComplexModelDto(bindingContext.ModelMetadata, bindingContext.PropertyMetadata.Values);
            ModelBindingContext subContext = new ModelBindingContext(bindingContext)
            {
                ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(() => dto, typeof(ComplexModelDto)),
                ModelName = bindingContext.ModelName
            };
            actionContext.Bind(subContext);

            foreach (KeyValuePair<ModelMetadata, ComplexModelDtoResult> item in dto.Results)
            {
                ModelMetadata propertyMetadata = item.Key;
                ComplexModelDtoResult dtoResult = item.Value;
                if (dtoResult != null)
                {
                    PropertyInfo propertyInfo = bindingContext.ModelType.GetProperty(propertyMetadata.PropertyName);
                    if (propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(bindingContext.Model, dtoResult.Model);
                    }
                }
            }
            return true;
        }
        public Tuple<string[], string[]> Get()
        {
            HttpActionContext actionContext = new HttpActionContext
            {
                ControllerContext = this.ControllerContext
            };
            ModelMetadataProvider metadataProvider = this.Configuration.Services.GetModelMetadataProvider();
            ModelMetadata metadata = metadataProvider.GetMetadataForType(null, typeof(DemoModel));
            IValueProvider valueProvider = new CompositeValueProviderFactory(this.Configuration.Services.GetValueProviderFactories()).GetValueProvider(actionContext);
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = metadata,
                ValueProvider = valueProvider,
                ModelState = actionContext.ModelState
            };
            actionContext.Bind(bindingContext);

            //验证之前的错误消息
            string[] errorMessages1 = actionContext.ModelState.SelectMany(
                item => item.Value.Errors.Select(
                error => error.ErrorMessage)).ToArray();

            //验证之前的错误消息
            bindingContext.ValidationNode.Validate(actionContext);
            string[] errorMessages2 = actionContext.ModelState.SelectMany(
                item => item.Value.Errors.Select(
                error => error.ErrorMessage)).ToArray();
            return new Tuple<string[], string[]>(errorMessages1, errorMessages2);
        }
 public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
 {
     ComplexModelDto dto = bindingContext.Model as ComplexModelDto;
     if (null == dto)
     {
         return false;
     }
     foreach (ModelMetadata property in dto.PropertyMetadata)
     {
         ModelBindingContext subContext = new ModelBindingContext(bindingContext)
         {
             ModelMetadata = property,
             ModelName = ModelNameBuilder.CreatePropertyModelName(bindingContext.ModelName, property.PropertyName)
         };
         if (actionContext.Bind(subContext))
         {
             dto.Results[property] = new ComplexModelDtoResult(subContext.Model, subContext.ValidationNode);
         }
     }
     return true;
 }
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(ComplexModelDto), false /* allowNullModel */);

            ComplexModelDto dto = (ComplexModelDto)bindingContext.Model;
            foreach (ModelMetadata propertyMetadata in dto.PropertyMetadata)
            {
                ModelBindingContext propertyBindingContext = new ModelBindingContext(bindingContext)
                {
                    ModelMetadata = propertyMetadata,
                    ModelName = ModelBindingHelper.CreatePropertyModelName(bindingContext.ModelName, propertyMetadata.PropertyName)
                };

                // bind and propagate the values
                // If we can't bind, then leave the result missing (don't add a null).
                if (actionContext.Bind(propertyBindingContext))                
                {
                    dto.Results[propertyMetadata] = new ComplexModelDtoResult(propertyBindingContext.Model, propertyBindingContext.ValidationNode);
                }                
            }

            return true;
        }
        private ComplexModelDto CreateAndPopulateDto(HttpActionContext actionContext, ModelBindingContext bindingContext, IEnumerable<ModelMetadata> propertyMetadatas)
        {
            ModelMetadataProvider metadataProvider = MetadataProvider ?? actionContext.GetMetadataProvider();

            // create a DTO and call into the DTO binder
            ComplexModelDto originalDto = new ComplexModelDto(bindingContext.ModelMetadata, propertyMetadatas);
            ModelBindingContext dtoBindingContext = new ModelBindingContext(bindingContext)
            {
                ModelMetadata = metadataProvider.GetMetadataForType(() => originalDto, typeof(ComplexModelDto)),
                ModelName = bindingContext.ModelName
            };

            actionContext.Bind(dtoBindingContext);
            return (ComplexModelDto)dtoBindingContext.Model;
        }
 private bool TryBind(HttpActionContext actionContext, ModelBindingContext bindingContext)
 {
     return actionContext.Bind(bindingContext, Binders);
 }