コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        public void Bind_IntArray_NoIndex()
        {
            var provider = new ValueProvider()
            {
                Parameters = { new Parameter("ages[]", "3", "20") }
            };
            var context = new ModelBinderContext(typeof(int[]), "ages", "", provider);

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

            Assert.NotNull(actual);
            Assert.Equal(3, actual[0]);
            Assert.Equal(20, actual[1]);
        }