// Used when the ValueProvider contains the collection to be bound as multiple elements, e.g. foo[0], foo[1]. private static List <TElement> BindComplexCollection(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { string indexPropertyName = ModelBinderUtil.CreatePropertyModelName(bindingContext.ModelName, "index"); ValueProviderResult vpResultIndex = bindingContext.ValueProvider.GetValue(indexPropertyName); IEnumerable <string> indexNames = CollectionModelBinderUtil.GetIndexNamesFromValueProviderResult(vpResultIndex); return(BindComplexCollectionFromIndexes(controllerContext, bindingContext, indexNames)); }
public override IExtensibleModelBinder GetBinder(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { ModelBinderUtil.ValidateBindingContext(bindingContext); if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName)) { return(CollectionModelBinderUtil.GetGenericBinder(typeof(IDictionary <,>), typeof(Dictionary <,>), typeof(DictionaryModelBinder <,>), bindingContext.ModelMetadata)); } else { return(null); } }
protected override bool CreateOrReplaceCollection( ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext, IList <KeyValuePair <TKey, TValue> > newCollection ) { CollectionModelBinderUtil.CreateOrReplaceDictionary( bindingContext, newCollection, () => new Dictionary <TKey, TValue>() ); return(true); }
// Extensibility point that allows the bound collection to be manipulated or transformed before // being returned from the binder. protected virtual bool CreateOrReplaceCollection( ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext, IList <TElement> newCollection ) { CollectionModelBinderUtil.CreateOrReplaceCollection( bindingContext, newCollection, () => new List <TElement>() ); return(true); }
internal static List <TElement> BindComplexCollectionFromIndexes(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext, IEnumerable <string> indexNames) { bool indexNamesIsFinite; if (indexNames != null) { indexNamesIsFinite = true; } else { indexNamesIsFinite = false; indexNames = CollectionModelBinderUtil.GetZeroBasedIndexes(); } List <TElement> boundCollection = new List <TElement>(); foreach (string indexName in indexNames) { string fullChildName = ModelBinderUtil.CreateIndexModelName(bindingContext.ModelName, indexName); ExtensibleModelBindingContext childBindingContext = new ExtensibleModelBindingContext(bindingContext) { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TElement)), ModelName = fullChildName }; object boundValue = null; IExtensibleModelBinder childBinder = bindingContext.ModelBinderProviders.GetBinder(controllerContext, childBindingContext); if (childBinder != null) { if (childBinder.BindModel(controllerContext, childBindingContext)) { boundValue = childBindingContext.Model; // merge validation up bindingContext.ValidationNode.ChildNodes.Add(childBindingContext.ValidationNode); } } else { // should we even bother continuing? if (!indexNamesIsFinite) { break; } } boundCollection.Add(ModelBinderUtil.CastOrDefault <TElement>(boundValue)); } return(boundCollection); }