当前位置: 首页>>代码示例>>C#>>正文


C# ExtensibleModelBindingContext类代码示例

本文整理汇总了C#中ExtensibleModelBindingContext的典型用法代码示例。如果您正苦于以下问题:C# ExtensibleModelBindingContext类的具体用法?C# ExtensibleModelBindingContext怎么用?C# ExtensibleModelBindingContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ExtensibleModelBindingContext类属于命名空间,在下文中一共展示了ExtensibleModelBindingContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BindModel

        public void BindModel() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
                ModelName = "someName",
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ValueProvider = new SimpleValueProvider() {
                    { "someName[0]", "42" },
                    { "someName[1]", "84" }
                }
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
                        return true;
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);

            // Act
            bool retVal = new ArrayModelBinder<int>().BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsTrue(retVal);

            int[] array = bindingContext.Model as int[];
            CollectionAssert.AreEqual(new int[] { 42, 84 }, array);
        }
开发者ID:pruiz,项目名称:AspMvc2,代码行数:32,代码来源:ArrayModelBinderTest.cs

示例2: TryBindStrongModel_BinderExists_BinderReturnsIncorrectlyTypedObject_ReturnsTrue

        public void TryBindStrongModel_BinderExists_BinderReturnsIncorrectlyTypedObject_ReturnsTrue() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
                ModelName = "someName",
                ModelState = new ModelStateDictionary(),
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ValueProvider = new SimpleValueProvider()
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        Assert.AreEqual("someName.key", mbc.ModelName);
                        return true;
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);

            // Act
            int model;
            bool retVal = KeyValuePairModelBinderUtil.TryBindStrongModel<int>(controllerContext, bindingContext, "key", new EmptyModelMetadataProvider(), out model);

            // Assert
            Assert.IsTrue(retVal);
            Assert.AreEqual(default(int), model);
            Assert.AreEqual(1, bindingContext.ValidationNode.ChildNodes.Count, "Child validation node should have been added.");
            Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState should remain unmodified.");
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:31,代码来源:KeyValuePairModelBinderUtilTest.cs

示例3: ModelBinderProvidersProperty

        public void ModelBinderProvidersProperty() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();

            // Act & assert
            MemberHelper.TestPropertyWithDefaultInstance(bindingContext, "ModelBinderProviders", new ModelBinderProviderCollection(), ModelBinderProviders.Providers);
        }
开发者ID:consumentor,项目名称:Server,代码行数:7,代码来源:ExtensibleModelBindingContextTest.cs

示例4: BindModel_MissingValue_ReturnsTrue

        public void BindModel_MissingValue_ReturnsTrue() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)),
                ModelName = "someName",
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ValueProvider = new SimpleValueProvider()
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        mbc.Model = 42;
                        return true;
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);

            KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>();

            // Act
            bool retVal = binder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsTrue(retVal, "Want to return 'True' since the key was bound correctly and we want to push validation up.");
            Assert.IsNull(bindingContext.Model);
            CollectionAssert.AreEquivalent(new string[] { "someName.key" },
                bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray());
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:31,代码来源:KeyValuePairModelBinderTest.cs

示例5: ModelNameProperty

        public void ModelNameProperty() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();

            // Act & assert
            MemberHelper.TestStringProperty(bindingContext, "ModelName", String.Empty);
        }
开发者ID:jesshaw,项目名称:ASP.NET-Mvc-3,代码行数:7,代码来源:ExtensibleModelBindingContextTest.cs

示例6: BindModel

        public void BindModel() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ModelMetadata = GetMetadataForObject(new Person()),
                ModelName = "someName"
            };

            Mock<IExtensibleModelBinder> mockDtoBinder = new Mock<IExtensibleModelBinder>();
            mockDtoBinder
                .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc2) {
                        return true; // just return the DTO unchanged
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(ComplexModelDto), mockDtoBinder.Object, true /* suppressPrefixCheck */);

            Mock<TestableMutableObjectModelBinder> mockTestableBinder = new Mock<TestableMutableObjectModelBinder>() { CallBase = true };
            mockTestableBinder.Expect(o => o.EnsureModelPublic(controllerContext, bindingContext)).Verifiable();
            mockTestableBinder.Expect(o => o.GetMetadataForPropertiesPublic(controllerContext, bindingContext)).Returns(new ModelMetadata[0]).Verifiable();
            TestableMutableObjectModelBinder testableBinder = mockTestableBinder.Object;
            testableBinder.MetadataProvider = new DataAnnotationsModelMetadataProvider();

            // Act
            bool retValue = testableBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsTrue(retValue);
            Assert.IsInstanceOfType(bindingContext.Model, typeof(Person));
            Assert.IsTrue(bindingContext.ValidationNode.ValidateAllProperties);
            mockTestableBinder.Verify();
        }
开发者ID:pruiz,项目名称:AspMvc2,代码行数:33,代码来源:MutableObjectModelBinderTest.cs

示例7: BindModel

        public void BindModel() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IDictionary<int, string>)),
                ModelName = "someName",
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ValueProvider = new SimpleValueProvider() {
                    { "someName[0]", new KeyValuePair<int, string>(42, "forty-two") },
                    { "someName[1]", new KeyValuePair<int, string>(84, "eighty-four") }
                }
            };

            Mock<IExtensibleModelBinder> mockKvpBinder = new Mock<IExtensibleModelBinder>();
            mockKvpBinder
                .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
                        return true;
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(KeyValuePair<int, string>), mockKvpBinder.Object, false /* suppressPrefixCheck */);

            // Act
            bool retVal = new DictionaryModelBinder<int, string>().BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsTrue(retVal);

            IDictionary<int, string> dictionary = bindingContext.Model as IDictionary<int, string>;
            Assert.IsNotNull(dictionary);
            Assert.AreEqual(2, dictionary.Count);
            Assert.AreEqual("forty-two", dictionary[42]);
            Assert.AreEqual("eighty-four", dictionary[84]);
        }
开发者ID:pruiz,项目名称:AspMvc2,代码行数:35,代码来源:DictionaryModelBinderTest.cs

示例8: BindComplexCollectionFromIndexes_InfiniteIndexes

        public void BindComplexCollectionFromIndexes_InfiniteIndexes() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
                ModelName = "someName",
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ValueProvider = new SimpleValueProvider() {
                    { "someName[0]", "42" },
                    { "someName[1]", "100" },
                    { "someName[3]", "400" }
                }
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
                        mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
                        return true;
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);

            // Act
            List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, null /* indexNames */);

            // Assert
            CollectionAssert.AreEqual(new int[] { 42, 100 }, boundCollection, "Binding should have halted at missing element.");
            CollectionAssert.AreEquivalent(new string[] { "someName[0]", "someName[1]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
        }
开发者ID:consumentor,项目名称:Server,代码行数:32,代码来源:CollectionModelBinderTest.cs

示例9: ModelNameProperty

        public void ModelNameProperty() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();

            // Act & assert
            MemberHelper.TestStringProperty(bindingContext, "ModelName", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
开发者ID:consumentor,项目名称:Server,代码行数:7,代码来源:ExtensibleModelBindingContextTest.cs

示例10: BindComplexCollectionFromIndexes_FiniteIndexes

        public void BindComplexCollectionFromIndexes_FiniteIndexes()
        {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
                ModelName = "someName",
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ValueProvider = new SimpleValueProvider
                {
                    { "someName[foo]", "42" },
                    { "someName[baz]", "200" }
                }
            };

            Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
            mockIntBinder
                .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
                .Returns(
                    delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
                    {
                        mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
                        return true;
                    });
            bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);

            // Act
            List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, new[] { "foo", "bar", "baz" });

            // Assert
            Assert.Equal(new[] { 42, 0, 200 }, boundCollection.ToArray());
            Assert.Equal(new[] { "someName[foo]", "someName[baz]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:35,代码来源:CollectionModelBinderTest.cs

示例11: ModelStateProperty

        public void ModelStateProperty() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
            ModelStateDictionary modelState = new ModelStateDictionary();

            // Act & assert
            MemberHelper.TestPropertyWithDefaultInstance(bindingContext, "ModelState", modelState);
        }
开发者ID:consumentor,项目名称:Server,代码行数:8,代码来源:ExtensibleModelBindingContextTest.cs

示例12: ModelNameProperty

        public void ModelNameProperty()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();

            // Act & assert
            Assert.Reflection.StringProperty(bindingContext, (context) => context.ModelName, String.Empty);
        }
开发者ID:huangw-t,项目名称:aspnetwebstack,代码行数:8,代码来源:ExtensibleModelBindingContextTest.cs

示例13: ModelProperty

        public void ModelProperty() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int))
            };

            // Act & assert
            MemberHelper.TestPropertyValue(bindingContext, "Model", 42);
        }
开发者ID:consumentor,项目名称:Server,代码行数:9,代码来源:ExtensibleModelBindingContextTest.cs

示例14: ModelProperty_ThrowsIfModelMetadataDoesNotExist

        public void ModelProperty_ThrowsIfModelMetadataDoesNotExist()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();

            // Act & assert
            Assert.Throws<InvalidOperationException>(
                delegate { bindingContext.Model = null; },
                "The ModelMetadata property must be set before accessing this property.");
        }
开发者ID:huangw-t,项目名称:aspnetwebstack,代码行数:10,代码来源:ExtensibleModelBindingContextTest.cs

示例15: GetBinder_NoPrefixInValueProvider_ReturnsNull

        public void GetBinder_NoPrefixInValueProvider_ReturnsNull() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => 42, typeof(int)),
                ModelName = "foo",
                ValueProvider = new SimpleValueProvider()
            };

            MutableObjectModelBinderProvider binderProvider = new MutableObjectModelBinderProvider();

            // Act
            IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext);

            // Assert
            Assert.IsNull(binder);
        }
开发者ID:jesshaw,项目名称:ASP.NET-Mvc-3,代码行数:16,代码来源:MutableObjectModelBinderProviderTest.cs


注:本文中的ExtensibleModelBindingContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。