// add property filter described by BindAttribute, override prefix
        protected override object BindCslaModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (string.IsNullOrEmpty(BindCriteria.Include) && string.IsNullOrEmpty(BindCriteria.Exclude) && string.IsNullOrEmpty(BindCriteria.Prefix))
            {
                return(base.BindCslaModel(controllerContext, bindingContext));
            }

            Predicate <string> propFilter = bindingContext.PropertyFilter;

            if (!string.IsNullOrEmpty(BindCriteria.Include) || !string.IsNullOrEmpty(BindCriteria.Exclude))
            {
                var bindAttr = new BindAttribute()
                {
                    Exclude = BindCriteria.Exclude, Include = BindCriteria.Include
                };
                propFilter = (propName) => bindAttr.IsPropertyAllowed(propName) &&
                             bindingContext.PropertyFilter(propName);
            }

            var newPrefix = BindCriteria.Prefix ?? bindingContext.ModelName;

            var newBindingContext = new ModelBindingContext()
            {
                Model          = bindingContext.Model,
                ModelName      = newPrefix,
                ModelState     = bindingContext.ModelState,
                ModelType      = bindingContext.ModelType,
                PropertyFilter = propFilter,
                ValueProvider  = bindingContext.ValueProvider
            };

            return(base.BindCslaModel(controllerContext, newBindingContext));
        }
예제 #2
0
        public void CopyConstructor()
        {
            // Arrange
            ModelBindingContext originalBindingContext = new ModelBindingContext()
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new EmptyModelMetadataProvider().GetMetadataForType(
                    null,
                    typeof(object)
                    ),
                ModelName      = "theName",
                ModelState     = new ModelStateDictionary(),
                PropertyFilter = _ => false,
                ValueProvider  = new SimpleValueProvider()
            };

            // Act
            ModelBindingContext newBindingContext = new ModelBindingContext(originalBindingContext);

            // Assert
            Assert.False(newBindingContext.FallbackToEmptyPrefix);
            Assert.Null(newBindingContext.ModelMetadata);
            Assert.Equal("", newBindingContext.ModelName);
            Assert.Equal(originalBindingContext.ModelState, newBindingContext.ModelState);
            Assert.True(newBindingContext.PropertyFilter("foo"));
            Assert.Equal(originalBindingContext.ValueProvider, newBindingContext.ValueProvider);
        }
예제 #3
0
 protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) {
     foreach (var validationResult in ModelValidator.GetModelValidator(bindingContext.ModelMetadata, controllerContext).Validate(null)) {
         string subPropertyName = CreateSubPropertyName(bindingContext.ModelName, validationResult.MemberName);
         if (bindingContext.PropertyFilter(subPropertyName)) {
             if (bindingContext.ModelState.IsValidField(subPropertyName)) {
                 bindingContext.ModelState.AddModelError(subPropertyName, validationResult.Message);
             }
         }
     }
 }
예제 #4
0
 private void AddRequestValues(JsonDictionary model, NameValueCollection nameValueCollection, ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     foreach (string key in nameValueCollection.Keys)
     {
         if (bindingContext.PropertyFilter(key))
         {
             var jsonText = nameValueCollection[key];
             var newModel = DeserializeJson(jsonText);
             // Add the new JSON key/value pairs to the Model
             model.Add(newModel);
         }
     }
 }
예제 #5
0
 private static void CheckPropertyFilter(ModelBindingContext bindingContext)
 {
     if (
         bindingContext.ModelType
         .GetProperties()
         .Select(p => p.Name)
         .Any(name => !bindingContext.PropertyFilter(name))
         )
     {
         throw new InvalidOperationException(
                   MvcResources.ExtensibleModelBinderAdapter_PropertyFilterMustNotBeSet
                   );
     }
 }
예제 #6
0
 private void DoFillModel(Dictionary <string, object> model, NameValueCollection nameValueCollection, ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     foreach (string key in nameValueCollection.Keys)
     {
         if (bindingContext.PropertyFilter(key))
         {
             string strValue = nameValueCollection[key];
             Dictionary <string, object> newModel = DoDeserializeString(strValue);
             if (string.IsNullOrEmpty(key))
             {
                 foreach (string k in newModel.Keys)
                 {
                     model.Add(k, newModel[k]);
                 }
             }
         }
     }
 }
예제 #7
0
        internal ModelBindingContext CreateComplexElementalModelBindingContext(ControllerContext controllerContext, ModelBindingContext bindingContext, object model)
        {
            BindAttribute      bindAttr          = (BindAttribute)GetTypeDescriptor(controllerContext, bindingContext).GetAttributes()[typeof(BindAttribute)];
            Predicate <string> newPropertyFilter = (bindAttr != null)
                ? propertyName => bindAttr.IsPropertyAllowed(propertyName) && bindingContext.PropertyFilter(propertyName)
                : bindingContext.PropertyFilter;

            ModelBindingContext newBindingContext = new ModelBindingContext()
            {
                ModelMetadata  = ModelMetadataProviders.Current.GetMetadataForType(() => model, bindingContext.ModelType),
                ModelName      = bindingContext.ModelName,
                ModelState     = bindingContext.ModelState,
                PropertyFilter = newPropertyFilter,
                ValueProvider  = bindingContext.ValueProvider
            };

            return(newBindingContext);
        }
        public void CopyConstructor()
        {
            // Arrange
            ModelBindingContext originalBindingContext = new ModelBindingContext()
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object)),
                ModelName = "theName",
                ModelState = new ModelStateDictionary(),
                PropertyFilter = _ => false,
                ValueProvider = new SimpleValueProvider()
            };

            // Act
            ModelBindingContext newBindingContext = new ModelBindingContext(originalBindingContext);

            // Assert
            Assert.False(newBindingContext.FallbackToEmptyPrefix);
            Assert.Null(newBindingContext.ModelMetadata);
            Assert.Equal("", newBindingContext.ModelName);
            Assert.Equal(originalBindingContext.ModelState, newBindingContext.ModelState);
            Assert.True(newBindingContext.PropertyFilter("foo"));
            Assert.Equal(originalBindingContext.ValueProvider, newBindingContext.ValueProvider);
        }
예제 #9
0
        public void CopyConstructor()
        {
            // Arrange
            ModelBindingContext originalBindingContext = new ModelBindingContext()
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object)),
                ModelName             = "theName",
                ModelState            = new ModelStateDictionary(),
                PropertyFilter        = _ => false,
                ValueProvider         = new SimpleValueProvider()
            };

            // Act
            ModelBindingContext newBindingContext = new ModelBindingContext(originalBindingContext);

            // Assert
            Assert.IsFalse(newBindingContext.FallbackToEmptyPrefix, "Property 'FallbackToEmptyPrefix' should not have been propagated.");
            Assert.IsNull(newBindingContext.ModelMetadata, "Property 'ModelMetadata' should not have been propagated.");
            Assert.AreEqual("", newBindingContext.ModelName, "Property 'ModelName' should not have been propagated.");
            Assert.AreEqual(originalBindingContext.ModelState, newBindingContext.ModelState, "Property 'ModelState' should have been propagated.");
            Assert.IsTrue(newBindingContext.PropertyFilter("foo"), "Property 'PropertyFilter' should not have been propagated.");
            Assert.AreEqual(originalBindingContext.ValueProvider, newBindingContext.ValueProvider, "Property 'ValueProvider' should have been propagated.");
        }