public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            ModelBindingContext context4key = new ModelBindingContext(bindingContext)
            {
                ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(null, typeof(TKey)),
                ModelName     = ModelNameBuilder.CreatePropertyModelName(bindingContext.ModelName, "key")
            };

            if (actionContext.Bind(context4key))
            {
                TKey key = (TKey)context4key.Model;
                ModelBindingContext context4Value = new ModelBindingContext(bindingContext)
                {
                    ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(null, typeof(TValue)),
                    ModelName     = ModelNameBuilder.CreatePropertyModelName(bindingContext.ModelName, "value")
                };
                if (actionContext.Bind(context4Value))
                {
                    TValue value = (TValue)context4Value.Model;
                    bindingContext.Model = new KeyValuePair <TKey, TValue>(key, value);
                    return(true);
                }
            }
            return(false);
        }
示例#2
0
        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);
        }
        private List <TElement> BindComplexCollection(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            string key = ModelNameBuilder.CreatePropertyModelName(bindingContext.ModelName, "index");
            ValueProviderResult  result = bindingContext.ValueProvider.GetValue(key);
            IEnumerable <string> indexes;
            bool useZeroBasedIndexes = false;

            if (null == result)
            {
                indexes             = this.GetZeroBasedIndexes();
                useZeroBasedIndexes = true;
            }
            else
            {
                indexes = (IEnumerable <string>)result.ConvertTo(typeof(IEnumerable <string>));
            }
            List <TElement> list = new List <TElement>();

            foreach (string index in indexes)
            {
                ModelBindingContext subContext = new ModelBindingContext(bindingContext)
                {
                    ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(null, typeof(TElement)),
                    ModelName     = ModelNameBuilder.CreateIndexModelName(bindingContext.ModelName, index)
                };
                bool failed = true;
                if (actionContext.Bind(subContext))
                {
                    list.Add((TElement)subContext.Model);
                    failed = false;
                }
                if (failed && useZeroBasedIndexes)
                {
                    return(list);
                }
            }
            return(list);
        }