예제 #1
0
        public void CanBind_WithPrefix_To_RootList()
        {
            var model  = RootList.Get(2);
            var values = new NameValueCollection();

            values.Add("pfx[0].AnyString", "Any-0");
            values.Add("pfx[0].Max5Chars", "Max5-0");
            values.Add("pfx[1].AnyString", "Any-1");
            values.Add("pfx[1].Max5Chars", "Max5-1");

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "pfx",
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(RootList)),
                ValueProvider = new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
            };
            var    binder   = new CslaModelBinder();
            object retModel = binder.BindModel(new ControllerContext(), bindingContext);

            Assert.AreSame(model, retModel, "Return should be the same as original");
            Assert.AreEqual("Any-0", model[0].AnyString);
            Assert.AreEqual("Max5-0", model[0].Max5Chars);
            Assert.AreEqual("Any-1", model[1].AnyString);
            Assert.AreEqual("Max5-1", model[1].Max5Chars);
        }
예제 #2
0
        public void Bind_RootList_WithInvalidValues_ModelState_ReturnsErrors()
        {
            var model  = RootList.Get(1);
            var values = new NameValueCollection();

            values.Add("[0].AnyString", "Any-0");
            values.Add("[0].Max5Chars", "More Than 5 Characters");

            var bindingContext = new ModelBindingContext
            {
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(RootList)),
                ValueProvider = new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
            };
            var binder = new CslaModelBinder();

            binder.BindModel(new ControllerContext(), bindingContext);

            Assert.IsFalse(model.IsValid, "Object should be invalid");
            Assert.IsFalse(bindingContext.ModelState.IsValid, "Model State should be invalid");
            Assert.IsFalse(bindingContext.ModelState.IsValidField("[0].Max5Chars"), "Max5Chars property should be invalid");
            Assert.AreEqual(1, bindingContext.ModelState["[0].Max5Chars"].Errors.Count);
        }