Beispiel #1
0
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     if (bindingContext.ModelType.IsInterface || bindingContext.ModelType.IsAbstract)
     {
         return Easy.Reflection.ClassAction.GetModel(ServiceLocator.Current.GetInstance(bindingContext.ModelType).GetType(), controllerContext.RequestContext.HttpContext.Request.Form);
     }
     else
     {
         DefaultModelBinder binder = new DefaultModelBinder();
         return binder.BindModel(controllerContext, bindingContext);
     }
 }
        public void all_parameters_are_bound()
        {
            var form = new FormCollection()
            {
                { "Name", "test_name" }
            };

            var context = new ModelBindingContext() { ValueProvider = form.ToValueProvider(), ModelType = typeof(CodeCamp) };

            var mb = new DefaultModelBinder();
            var codeCamp = (CodeCamp) mb.BindModel(null, context);

            Assert.That(codeCamp.Name, Is.EqualTo("test_name"));
        }
        public string Get(ControllerContext controllerContext)
        {
            var defaultModelBinder = new DefaultModelBinder();

            var modelType = typeof(GiftCardInfo);
            var giftCardInfo = new GiftCardInfo();
            var metadataForType = ModelMetadataProviders.Current.GetMetadataForType(() => giftCardInfo, modelType);
            metadataForType.Model = giftCardInfo;
            var modelBindingContext = new ModelBindingContext
            {
                ModelMetadata = metadataForType,
                ValueProvider = new FormValueProvider(controllerContext)
            };
            var model = defaultModelBinder.BindModel(controllerContext, modelBindingContext) as GiftCardInfo;

            return model == null ? null : JsonConvert.SerializeObject(model);
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            DefaultModelBinder binder = new DefaultModelBinder();	// use the default model binder to do the dirty work

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

            ImageList list = binder.BindModel(controllerContext, listBindingContext) as ImageList;

            return list;
        }
Beispiel #5
0
        public static object ConvertToObject(this FormDataCollection formDataCollection, Type type)
        {
            try
            {
                DefaultModelBinder binder = new DefaultModelBinder();
                ModelBindingContext modelBindingContext = new ModelBindingContext()
                {
                    ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, type),
                    ValueProvider = new FormValueProvider(formDataCollection.ReadAsNameValueCollection())
                };
                return binder.BindModel(new ControllerContext(), modelBindingContext);
            }
            catch (Exception)
            {

                return null;
            }
        }
        public void BindModelReturnsNullIfSimpleTypeNotFound() {
            // DevDiv 216165: ModelBinders should not try and instantiate simple types

            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext() {
                ModelName = "prefix",
                ModelType = typeof(string),
                ValueProvider = new ValueProviderDictionary(null) {
                    { "prefix.foo", new ValueProviderResult("foo", "foo", null) },
                    { "prefix.bar", new ValueProviderResult("bar", "bar", null) }
                }
            };

            DefaultModelBinder binder = new DefaultModelBinder();

            // Act
            object updatedModel = binder.BindModel(null, bindingContext);

            // Assert
            Assert.IsNull(updatedModel);
        }
        public void BindModelWithPrefixReturnsNullIfFallbackNotSpecifiedAndValueProviderContainsNoEntries() {
            // Arrange
            ModelWithoutBindAttribute model = new ModelWithoutBindAttribute() {
                Foo = "FooPreValue",
                Bar = "BarPreValue",
                Baz = "BazPreValue",
            };
            ModelBindingContext bindingContext = new ModelBindingContext() {
                Model = model,
                ModelName = "prefix",
                ModelType = typeof(ModelWithoutBindAttribute),
                ValueProvider = new ValueProviderDictionary(null) {
                    { "foo", new ValueProviderResult("FooPostValue", "FooPostValue", null) },
                    { "bar", new ValueProviderResult("BarPostValue", "BarPostValue", null) }
                }
            };

            DefaultModelBinder binder = new DefaultModelBinder();

            // Act
            object updatedModel = binder.BindModel(null, bindingContext);

            // Assert
            Assert.IsNull(updatedModel);
        }
        public void BindModelWithPrefixAndFallback() {
            // Arrange
            ModelWithoutBindAttribute model = new ModelWithoutBindAttribute() {
                Foo = "FooPreValue",
                Bar = "BarPreValue",
                Baz = "BazPreValue",
            };
            ModelBindingContext bindingContext = new ModelBindingContext() {
                FallbackToEmptyPrefix = true,
                Model = model,
                ModelName = "prefix",
                ModelType = typeof(ModelWithoutBindAttribute),
                ValueProvider = new ValueProviderDictionary(null) {
                    { "foo", new ValueProviderResult("FooPostValue", "FooPostValue", null) },
                    { "bar", new ValueProviderResult("BarPostValue", "BarPostValue", null) }
                }
            };

            DefaultModelBinder binder = new DefaultModelBinder();

            // Act
            object updatedModel = binder.BindModel(null, bindingContext);

            // Assert
            Assert.AreSame(model, updatedModel, "Should have returned same instance of the model.");
            Assert.AreEqual("FooPostValue", model.Foo, "Foo property should have been updated.");
            Assert.AreEqual("BarPostValue", model.Bar, "Bar property should have been updated.");
            Assert.AreEqual("BazPreValue", model.Baz, "Baz property shouldn't have been updated since it wasn't part of the request.");
        }
        public void BindModelThrowsIfBindingContextIsNull() {
            // Arrange
            DefaultModelBinder binder = new DefaultModelBinder();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    binder.BindModel(null, null);
                }, "bindingContext");
        }
        public void BindModelReturnsNullIfKeyNotFound() {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext() {
                ModelName = "foo",
                ModelType = typeof(int),
                ValueProvider = new Dictionary<string, ValueProviderResult>()
            };

            DefaultModelBinder binder = new DefaultModelBinder();

            // Act
            object returnedModel = binder.BindModel(null, bindingContext);

            // Assert
            Assert.IsNull(returnedModel);
        }
        public void BindModelCanBindSimpleTypes() {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext() {
                ModelName = "foo",
                ModelType = typeof(int),
                ValueProvider = new Dictionary<string, ValueProviderResult>() { { "foo", new ValueProviderResult("42", "42", null) } }
            };

            DefaultModelBinder binder = new DefaultModelBinder();

            // Act
            object updatedModel = binder.BindModel(null, bindingContext);

            // Assert
            Assert.AreEqual(42, updatedModel);
        }
        public void BindModelCanBindObjects() {
            // Arrange
            ControllerContext controllerContext = new Mock<ControllerContext>().Object;

            ModelWithoutBindAttribute model = new ModelWithoutBindAttribute() {
                Foo = "FooPreValue",
                Bar = "BarPreValue",
                Baz = "BazPreValue",
            };
            ModelBindingContext bindingContext = new ModelBindingContext() {
                Model = model,
                ModelType = typeof(ModelWithoutBindAttribute),
                ValueProvider = new Dictionary<string, ValueProviderResult>() { { "Foo", null }, { "Bar", null } }
            };

            Mock<IModelBinder> mockInnerBinder = new Mock<IModelBinder>();
            mockInnerBinder
                .Expect(b => b.BindModel(It.IsAny<ControllerContext>(), It.IsAny<ModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ModelBindingContext bc) {
                        Assert.AreEqual(controllerContext, cc, "ControllerContext was not forwarded correctly.");
                        Assert.AreEqual(bindingContext.ValueProvider, bc.ValueProvider, "Value provider was not forwarded correctly.");
                        return bc.ModelName + "PostValue";
                    });

            DefaultModelBinder binder = new DefaultModelBinder() {
                Binders = new ModelBinderDictionary() {
                    { typeof(string), mockInnerBinder.Object }
                }
            };

            // Act
            object updatedModel = binder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreSame(model, updatedModel, "Should have returned same instance of the model.");
            Assert.AreEqual("FooPostValue", model.Foo, "Foo property should have been updated.");
            Assert.AreEqual("BarPostValue", model.Bar, "Bar property should have been updated.");
            Assert.AreEqual("BazPreValue", model.Baz, "Baz property shouldn't have been updated since it wasn't part of the request.");
        }
Beispiel #13
0
 public static IEnumerable<TextContent> GetCategories(string modelName, ControllerContext controllerContext, NameValueCollection formValues)
 {
     var modelState = new ModelStateDictionary();
     ModelBindingContext bindingContext = new ModelBindingContext()
     {
         FallbackToEmptyPrefix = true,
         ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(CategoryContent[])),
         ModelName = modelName,
         ModelState = modelState,
         ValueProvider = new ValueProviderCollection(new List<System.Web.Mvc.IValueProvider>() { new NameValueCollectionValueProvider(formValues, formValues, CultureInfo.CurrentCulture), controllerContext.Controller.ValueProvider })
     };
     DefaultModelBinder modelBinder = new DefaultModelBinder();
     var model = modelBinder.BindModel(controllerContext, bindingContext);
     if (model == null)
     {
         return null;
     }
     return ((IEnumerable<CategoryContent>)model).Select(it => new TextContent() { FolderName = it.FolderName, UUID = it.UUID });
 }