// 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);
        }
 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);
     }
 }
        internal void BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, object model)
        {
            // need to replace the property filter + model object and create an inner binding context
            BindAttribute      bindAttr          = (BindAttribute)TypeDescriptor.GetAttributes(bindingContext.ModelType)[typeof(BindAttribute)];
            Predicate <string> newPropertyFilter = (bindAttr != null)
                ? propertyName => bindAttr.IsPropertyAllowed(propertyName) && bindingContext.PropertyFilter(propertyName)
                : bindingContext.PropertyFilter;

            ModelBindingContext newBindingContext = new ModelBindingContext()
            {
                Model          = model,
                ModelName      = bindingContext.ModelName,
                ModelState     = bindingContext.ModelState,
                ModelType      = bindingContext.ModelType,
                PropertyFilter = newPropertyFilter,
                ValueProvider  = bindingContext.ValueProvider
            };

            // validation
            if (OnModelUpdating(controllerContext, newBindingContext))
            {
                BindProperties(controllerContext, newBindingContext);
                OnModelUpdated(controllerContext, newBindingContext);
            }
        }
        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.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.");
        }
 private IEnumerable<string> GetKeys(ModelBindingContext bindingContext)
 {
     var keys = new List<string> { bindingContext.ModelName, bindingContext.ModelName + ".Id", bindingContext.ModelName + "Id" };
     return keys.Where(x => bindingContext.PropertyFilter(x));
 }
        private 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;
        }