コード例 #1
0
ファイル: Modeltest.cs プロジェクト: monstrU/GuestBookMVC
        public void DecimalValidateTest()
        {

            var formCollection = new NameValueCollection { 
                    { "value", "2" }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(decimal));

            var bindingContext = new ModelBindingContext
            {
                ModelName = "decimal",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };
            DecimalModelBinder b1 = new DecimalModelBinder();
            ControllerContext controllerContext = new ControllerContext();

            var result = (decimal)b1.BindModel(controllerContext, bindingContext);

            //Assert.IsFalse(state.IsValid);
        }
コード例 #2
0
ファイル: Modeltest.cs プロジェクト: monstrU/GuestBookMVC
        public void ModelValidateDecTestCommaInRussian()
        {
            var formCollection = new NameValueCollection { 
                    { "Weight", "2,12" }
            };


            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(MessageModel));

            var bindingContext = new ModelBindingContext
            {
                ModelName = "Weight",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };
            Thread.CurrentThread.CurrentCulture = GetRussianCulture();

            ControllerContext controllerContext = new ControllerContext();
            DecimalModelBinder b1 = new DecimalModelBinder();
            var result = b1.BindModel(controllerContext, bindingContext);


            Assert.IsNotNull(result);
            Assert.IsTrue(result is decimal);
            Assert.IsTrue((decimal)result == (decimal)2.12);

            var state = bindingContext.ModelState;
            Assert.IsTrue(state.IsValid);

        }
コード例 #3
0
ファイル: Modeltest.cs プロジェクト: monstrU/GuestBookMVC
        public void ModelValidateDecTestFailRussian()
        {
            
            const string modelName = "Weight";
            var formCollection = new NameValueCollection { 
                    { modelName, "123qwe" }
            };


            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(MessageModel));

            var bindingContext = new ModelBindingContext
            {
                ModelName = modelName,
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };


            Thread.CurrentThread.CurrentCulture = GetRussianCulture();

            ControllerContext controllerContext = new ControllerContext();
            DecimalModelBinder b1 = new DecimalModelBinder();
            var result = b1.BindModel(controllerContext, bindingContext);


            Assert.IsNull(result);
            var state = bindingContext.ModelState;
            Assert.IsFalse(state.IsValid);
            Assert.IsFalse(state.IsValidField(modelName));


        }
コード例 #4
0
ファイル: Modeltest.cs プロジェクト: monstrU/GuestBookMVC
        public void ModelValidateTestPointNautral()
        {
            const string pointDecSeparator = ".";
            const string modelName = "Weight";
            var formCollection = new NameValueCollection { 
                    { modelName, "2.12" }
            };


            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(MessageModel));

            var bindingContext = new ModelBindingContext
            {
                ModelName = modelName,
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            
            Thread.CurrentThread.CurrentCulture = GetNeutralCulture(pointDecSeparator);

            ControllerContext controllerContext = new ControllerContext();
            DecimalModelBinder b1 = new DecimalModelBinder();
            var result = b1.BindModel(controllerContext, bindingContext);


            Assert.IsNotNull(result);
            Assert.IsTrue(result is decimal);
            Assert.IsTrue((decimal)result == (decimal)2.12);

            var state = bindingContext.ModelState;
            Assert.IsTrue(state.IsValid);
            Assert.IsTrue(state.IsValidField(modelName));
            

        }