/// <summary>
        /// Create a scope for a child model
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="modelName">Name of the model.</param>
        /// <returns></returns>
        private IModelBinderContext CreateForChild(Type type, string prefix, string modelName)
        {
            var context = new ModelBinderContext(type, modelName, prefix, ValueProvider);

            context.RootBinder = RootBinder;
            return(context);
        }
        public void Number_Ok()
        {
            var provider = new ValueProvider() { Parameters = { new Parameter("code", "404") } };
            var context = new ModelBinderContext(typeof(HttpStatusCode), "code", "", provider);

            var binder = new EnumModelBinder();
            var actual = (HttpStatusCode)binder.Bind(context);

            Assert.Equal(HttpStatusCode.NotFound, actual);
        }
        public void String_Ok()
        {
            var provider = new ValueProvider() { Parameters = { new Parameter("code", "Forbidden") } };
            var context = new ModelBinderContext(typeof(HttpStatusCode), "code", "", provider);

            var binder = new EnumModelBinder();
            var actual = (HttpStatusCode)binder.Bind(context);

            Assert.Equal(HttpStatusCode.Forbidden, actual);
        }
 public void Bind_IntArray_Gap()
 {
     var provider = new ValueProvider() { Parameters = { new Parameter("ages[2]", "3"), new Parameter("ages[0]", "20") } };
     var context = new ModelBinderContext(typeof(int[]), "ages", "", provider);
     context.RootBinder = new PrimitiveModelBinder();
     
     var binder = new ArrayModelBinder();
     
     Assert.Throws<ModelBindingException>(() => binder.Bind(context));
 }
        public void String()
        {
            var provider = new ValueProvider() { Parameters = { new Parameter("age", "Why do you need to know???") } };
            var context = new ModelBinderContext(typeof(string), "age", "", provider);

            var binder = new PrimitiveModelBinder();
            var actual = (string)binder.Bind(context);

            Assert.Equal("Why do you need to know???", actual);
        }
        public void Int()
        {
            var provider = new ValueProvider() { Parameters = { new Parameter("age", "3")}};
            var context = new ModelBinderContext(typeof(int), "age", "", provider);

            var binder = new PrimitiveModelBinder();
            var actual = (int)binder.Bind(context);

            Assert.Equal(3, actual);
        }
        public void Double()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(1033);
            var provider = new ValueProvider() { Parameters = { new Parameter("age", "3.5") } };
            var context = new ModelBinderContext(typeof(double), "age", "", provider);

            var binder = new PrimitiveModelBinder();
            var actual = (double)binder.Bind(context);

            Assert.Equal(3.5, actual);
        }
        public void Bind_IntArray_Index()
        {
            var provider = new ValueProvider() { Parameters = { new Parameter("ages[1]", "3"), new Parameter("ages[0]", "20") } };
            var context = new ModelBinderContext(typeof(int[]), "ages", "", provider);
            context.RootBinder = new PrimitiveModelBinder();
            var binder = new ArrayModelBinder();
            var actual = (int[])binder.Bind(context);

            Assert.NotNull(actual);
            Assert.Equal(3, actual[1]);
            Assert.Equal(20, actual[0]);
        }
        public void NoPublicDefaultConstructor()
        {
            var provider = new ValueProvider
            {
                Parameters =
                        {
                            new Parameter("user.FirstName", "jonas"),
                        }
            };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            var context = new ModelBinderContext(typeof(Test), "user", "", provider) { RootBinder = mapper };

            var binder = new ClassBinder();

            Assert.Throws<ModelBindingException>(() => binder.Bind(context));
        }
        public void InvalidFieldType()
        {
            var provider = new ValueProvider
            {
                Parameters =
                        {
                            new Parameter("user.FirstName", "jonas"),
                            new Parameter("user.Age", "arne")
                        }
            };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            var context = new ModelBinderContext(typeof(UserViewModel), "user", "", provider) { RootBinder = mapper };

            var binder = new ClassBinder();

            Assert.Throws<ModelBindingException>(() => binder.Bind(context));
        }
        public void UnknownField()
        {
            var provider = new ValueProvider
            {
                Parameters =
                        {
                            new Parameter("user.FirstName", "jonas"),
                            new Parameter("user.NotInventedHere", "23")
                        }
            };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            var context = new ModelBinderContext(typeof(UserViewModel), "user", "", provider) { RootBinder = mapper };

            var binder = new ClassBinder();
            var actual = (UserViewModel)binder.Bind(context);

            Assert.Equal("jonas", actual.FirstName);
        }
Exemple #12
0
        public T Bind <T>(IHttpRequest request, string name)
        {
            var provider = new RequestValueProvider(request);

            if (!string.IsNullOrEmpty(name))
            {
                var context = new ModelBinderContext(typeof(T), name, "", provider);
                context.RootBinder = this;
                foreach (var modelBinder in _binders)
                {
                    if (modelBinder.CanBind(context))
                    {
                        return((T)modelBinder.Bind(context));
                    }
                }

                return(default(T));
            }
            if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
            {
                throw new InvalidOperationException("did not expect IEnumerable implementations without a name in the binder.");
            }

            var model = Activator.CreateInstance(typeof(T));

            foreach (var property in model.GetType().GetProperties())
            {
                var context = new ModelBinderContext(property.PropertyType, property.Name, "", provider);
                context.RootBinder = this;
                foreach (var modelBinder in _binders)
                {
                    if (modelBinder.CanBind(context))
                    {
                        var value = modelBinder.Bind(context);
                        property.SetValue(model, value, null);
                        break;
                    }
                }
            }
            return((T)model);
        }
        public void Bind_ViewModelArray_Index()
        {
            var provider = new ValueProvider
                {
                    Parameters =
                        {
                            new Parameter("user[1].FirstName", "jonas"),
                            new Parameter("user[0].FirstName", "Arne"),
                            new Parameter("user[0].Age", "32"),
                            new Parameter("user[1].Age", "23")
                        }
                };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            mapper.AddBinder(new ClassBinder());
            var context = new ModelBinderContext(typeof(UserViewModel[]), "user", "", provider) {RootBinder = mapper};

            var binder = new ArrayModelBinder();
            var actual = (UserViewModel[])binder.Bind(context);

            Assert.NotNull(actual);
            Assert.Equal("jonas", actual[1].FirstName);
            Assert.Equal("Arne", actual[0].FirstName);
            Assert.Equal(23, actual[1].Age);
            Assert.Equal(32, actual[0].Age);
        }
        public void Bind_ViewModelArray_Gap()
        {
            var provider = new ValueProvider
            {
                Parameters =
                        {
                            new Parameter("user[1].FirstName", "jonas"),
                            new Parameter("user[3].FirstName", "Arne"),
                            new Parameter("user[0].Age", "32"),
                            new Parameter("user[1].Age", "23")
                        }
            };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            mapper.AddBinder(new ClassBinder());
            var context = new ModelBinderContext(typeof(UserViewModel[]), "user", "", provider) { RootBinder = mapper };

            var binder = new ArrayModelBinder();

            Assert.Throws<ModelBindingException>(() => binder.Bind(context));
        }  
 /// <summary>
 /// Create a scope for a child model
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="prefix">The prefix.</param>
 /// <param name="modelName">Name of the model.</param>
 /// <returns></returns>
 private IModelBinderContext CreateForChild(Type type, string prefix, string modelName)
 {
     var context = new ModelBinderContext(type, modelName, prefix, ValueProvider);
     context.RootBinder = RootBinder;
     return context;
 }