public void DoesAnyKeyHavePrefixSuccess() { // Arrange Dictionary <string, object> dict = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase) { { "FOO.BAR", 42 } }; // Act bool wasPrefixFound = DictionaryHelpers.DoesAnyKeyHavePrefix(dict, "foo"); // Assert Assert.True(wasPrefixFound); }
private object BindCslaCollectionModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var collection = (IList)bindingContext.Model; for (int idx = 0; ; idx++) { var subIndexKey = CreateSubIndexName(bindingContext.ModelName, idx); // TODO: investigagte when sub index not exist but element model exist, if this model should be deleted // if no more value element to work with, exit. if (!DictionaryHelpers.DoesAnyKeyHavePrefix(bindingContext.ValueProvider, subIndexKey)) { break; } // TODO: investigate way to append new element in BindCslaCollectionModel // for now, we're done when no collection element to update if (idx >= collection.Count) { break; } object elementModel = collection[idx]; var innerContext = new ModelBindingContext() { Model = elementModel, ModelName = subIndexKey, ModelState = bindingContext.ModelState, ModelType = elementModel.GetType(), PropertyFilter = bindingContext.PropertyFilter, ValueProvider = bindingContext.ValueProvider }; BindProperties(controllerContext, innerContext); } return(bindingContext.Model); }
protected virtual object BindCslaModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // BindComplexModel should be made overridable so the following code is more appropriate there. object model = bindingContext.Model ?? CreateCslaModel(controllerContext, bindingContext, bindingContext.ModelType); // We also need to repeat the fallback to empty prefix (ModelName) before continue bool usePrefix = true; if (!string.IsNullOrEmpty(bindingContext.ModelName) && !DictionaryHelpers.DoesAnyKeyHavePrefix(bindingContext.ValueProvider, bindingContext.ModelName)) { if (!bindingContext.FallbackToEmptyPrefix) { return(null); } usePrefix = false; } var newBindingContext = new ModelBindingContext() { Model = model, ModelName = usePrefix ? bindingContext.ModelName : string.Empty, ModelState = bindingContext.ModelState, ModelType = bindingContext.ModelType, PropertyFilter = bindingContext.PropertyFilter, ValueProvider = bindingContext.ValueProvider }; if (typeof(Csla.Core.IEditableCollection).IsAssignableFrom((bindingContext.ModelType))) { return(BindCslaCollectionModel(controllerContext, newBindingContext)); } // bind IEditableBusinessObject BindProperties(controllerContext, newBindingContext); return(model); }