コード例 #1
0
        public void BindingEmptyValuesReturnsEmptyChoices()
        {
            //// Arrange

            var modelBinder       = new MvcBootstrapModelBinder();
            var controllerContext = new ControllerContext();

            var formCollection = new NameValueCollection
            {
                // Nothing
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Choices <TestEntityViewModel>));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = string.Empty,
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            //// Act

            var binding = modelBinder.BindModel(controllerContext, bindingContext) as Choices <TestEntityViewModel>;

            //// Assert

            Assert.That(binding, Is.Not.Null);
            Assert.That(binding.Selections, Is.Not.Null);
            Assert.That(binding.Selections, Is.Empty);
        }
コード例 #2
0
        public void BindingNonEmptyValuesReturnsCorrectChoice()
        {
            //// Arrange

            var modelBinder       = new MvcBootstrapModelBinder();
            var controllerContext = new ControllerContext();

            int id             = 1;
            var formCollection = new NameValueCollection
            {
                { "Choice", id.ToString(CultureInfo.InvariantCulture) }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Choice <TestEntityViewModel>));

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

            //// Act

            var binding = modelBinder.BindModel(controllerContext, bindingContext) as Choice <TestEntityViewModel>;

            //// Assert

            Assert.That(binding, Is.Not.Null);
            Assert.That(binding.Selection, Is.Not.Null);
            Assert.That(binding.Selection.Id, Is.EqualTo(id));
        }
コード例 #3
0
        public void BindingNonEmptyResultsReturnsCorrectChoices()
        {
            //// Arrange

            var modelBinder       = new MvcBootstrapModelBinder();
            var controllerContext = new ControllerContext();

            var ids            = new[] { 1, 2, 3 };
            var formCollection = new NameValueCollection
            {
                { "Choices.Id", ids[0].ToString(CultureInfo.InvariantCulture) },
                { "Choices.Id", ids[1].ToString(CultureInfo.InvariantCulture) },
                { "Choices.Id", ids[2].ToString(CultureInfo.InvariantCulture) }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Choices <TestEntityViewModel>));

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

            //// Act

            var binding = modelBinder.BindModel(controllerContext, bindingContext) as Choices <TestEntityViewModel>;

            //// Assert

            Assert.That(binding, Is.Not.Null);
            Assert.That(binding.Selections, Is.Not.Null);
            Assert.That(binding.Selections, Is.Not.Empty);
            Assert.That(binding.Selections.Select(vm => vm.Id), Is.EqualTo(ids));
        }