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


C# ModelValidationNode.Validate方法代码示例

本文整理汇总了C#中ModelValidationNode.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# ModelValidationNode.Validate方法的具体用法?C# ModelValidationNode.Validate怎么用?C# ModelValidationNode.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ModelValidationNode的用法示例。


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

示例1: SetProperty_PropertyIsSettable_CallsSetter

        public void SetProperty_PropertyIsSettable_CallsSetter()
        {
            // Arrange
            Person model = new Person();
            ControllerContext controllerContext = new ControllerContext
            {
                Controller = new EmptyController()
            };
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
            {
                ModelMetadata = GetMetadataForObject(model)
            };

            ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfBirth");
            ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
            ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(new DateTime(2001, 1, 1), validationNode);

            TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);

            // Assert
            validationNode.Validate(controllerContext);
            Assert.True(controllerContext.Controller.ViewData.ModelState.IsValid);
            Assert.Equal(new DateTime(2001, 1, 1), model.DateOfBirth);
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:27,代码来源:MutableObjectModelBinderTest.cs

示例2: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorNotPresent_RegistersValidationCallback

        public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorNotPresent_RegistersValidationCallback()
        {
            // Arrange
            ControllerContext controllerContext = new ControllerContext
            {
                Controller = new EmptyController()
            };
            ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
            {
                ModelMetadata = GetMetadataForObject(new Person()),
            };

            ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfBirth");
            ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
            ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);

            TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);

            // Assert
            Assert.True(controllerContext.Controller.ViewData.ModelState.IsValid);
            validationNode.Validate(controllerContext, bindingContext.ValidationNode);
            Assert.False(controllerContext.Controller.ViewData.ModelState.IsValid);
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:26,代码来源:MutableObjectModelBinderTest.cs

示例3: Validate_ValidateAllProperties_AddsValidationErrors

        public void Validate_ValidateAllProperties_AddsValidationErrors() {
            // Arrange
            ValidateAllPropertiesModel model = new ValidateAllPropertiesModel() {
                RequiredString = null /* error */,
                RangedInt = 0 /* error */,
                ValidString = "dog"
            };

            ModelMetadata modelMetadata = GetModelMetadata(model);
            ControllerContext controllerContext = new ControllerContext() {
                Controller = new EmptyController()
            };
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey") {
                ValidateAllProperties = true
            };

            controllerContext.Controller.ViewData.ModelState.AddModelError("theKey.RequiredString.Dummy", "existing Error Text");

            // Act
            node.Validate(controllerContext);

            // Assert
            Assert.IsNull(controllerContext.Controller.ViewData.ModelState["theKey.RequiredString"]);
            Assert.AreEqual("existing Error Text", controllerContext.Controller.ViewData.ModelState["theKey.RequiredString.Dummy"].Errors[0].ErrorMessage);
            Assert.AreEqual("The field RangedInt must be between 10 and 30.", controllerContext.Controller.ViewData.ModelState["theKey.RangedInt"].Errors[0].ErrorMessage);
            Assert.IsNull(controllerContext.Controller.ViewData.ModelState["theKey.ValidString"]);
            Assert.IsNull(controllerContext.Controller.ViewData.ModelState["theKey"]);
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:28,代码来源:ModelValidationNodeTest.cs

示例4: CombineWith

        public void CombineWith() {
            // Arrange
            List<string> log = new List<string>();

            ModelValidationNode[] allChildNodes = new ModelValidationNode[] {
                new ModelValidationNode(GetModelMetadata(), "key1"),
                new ModelValidationNode(GetModelMetadata(), "key2"),
                new ModelValidationNode(GetModelMetadata(), "key3"),
            };

            ModelValidationNode parentNode1 = new ModelValidationNode(GetModelMetadata(), "parent1");
            parentNode1.ChildNodes.Add(allChildNodes[0]);
            parentNode1.Validating += delegate { log.Add("Validating parent1."); };
            parentNode1.Validated += delegate { log.Add("Validated parent1."); };

            ModelValidationNode parentNode2 = new ModelValidationNode(GetModelMetadata(), "parent2");
            parentNode2.ChildNodes.Add(allChildNodes[1]);
            parentNode2.ChildNodes.Add(allChildNodes[2]);
            parentNode2.Validating += delegate { log.Add("Validating parent2."); };
            parentNode2.Validated += delegate { log.Add("Validated parent2."); };

            // Act
            parentNode1.CombineWith(parentNode2);
            parentNode1.Validate(new ControllerContext() { Controller = new EmptyController() });

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "Validating parent1.", "Validating parent2.", "Validated parent1.", "Validated parent2." },
                log);
            CollectionAssert.AreEqual(allChildNodes, parentNode1.ChildNodes.ToArray());
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:31,代码来源:ModelValidationNodeTest.cs

示例5: Validate_SkipsValidationIfSuppressed

        public void Validate_SkipsValidationIfSuppressed() {
            // Arrange
            List<string> log = new List<string>();
            LoggingDataErrorInfoModel model = new LoggingDataErrorInfoModel(log);
            ModelMetadata modelMetadata = GetModelMetadata(model);

            ControllerContext controllerContext = new ControllerContext() {
                Controller = new EmptyController()
            };
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey") {
                SuppressValidation = true
            };

            node.Validating += (sender, e) => {
                log.Add("In OnValidating()");
            };
            node.Validated += delegate {
                log.Add("In OnValidated()");
            };

            // Act
            node.Validate(controllerContext);

            // Assert
            CollectionAssert.AreEqual(new string[0], log, "Validate() should have been a no-op.");
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:26,代码来源:ModelValidationNodeTest.cs

示例6: Validate_ThrowsIfControllerContextIsNull

        public void Validate_ThrowsIfControllerContextIsNull() {
            // Arrange
            ModelValidationNode node = new ModelValidationNode(GetModelMetadata(), "someKey");

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    node.Validate(null);
                }, "controllerContext");
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:10,代码来源:ModelValidationNodeTest.cs

示例7: Validate_SkipsValidationIfHandlerCancels

        public void Validate_SkipsValidationIfHandlerCancels() {
            // Arrange
            List<string> log = new List<string>();
            LoggingDataErrorInfoModel model = new LoggingDataErrorInfoModel(log);
            ModelMetadata modelMetadata = GetModelMetadata(model);

            ControllerContext controllerContext = new ControllerContext() {
                Controller = new EmptyController()
            };
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey");

            node.Validating += (sender, e) => {
                log.Add("In OnValidating()");
                e.Cancel = true;
            };
            node.Validated += delegate {
                log.Add("In OnValidated()");
            };

            // Act
            node.Validate(controllerContext);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "In OnValidating()" },
                log);
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:27,代码来源:ModelValidationNodeTest.cs

示例8: Validate_SkipsRemainingValidationIfModelStateIsInvalid

        public void Validate_SkipsRemainingValidationIfModelStateIsInvalid() {
            // Because a property validator fails, the model validator shouldn't run

            // Arrange
            List<string> log = new List<string>();
            LoggingDataErrorInfoModel model = new LoggingDataErrorInfoModel(log);
            ModelMetadata modelMetadata = GetModelMetadata(model);

            ControllerContext controllerContext = new ControllerContext() {
                Controller = new EmptyController()
            };
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey");

            ModelMetadata childMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(() => model, model.GetType(), "InvalidStringProperty");
            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.InvalidStringProperty"));

            node.Validating += delegate {
                log.Add("In OnValidating()");
            };
            node.Validated += delegate {
                log.Add("In OnValidated()");
            };

            // Act
            node.Validate(controllerContext);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "In OnValidating()", "In IDataErrorInfo.get_Item('InvalidStringProperty')", "In OnValidated()" },
                log);

            Assert.AreEqual("Sample error message", controllerContext.Controller.ViewData.ModelState["theKey.InvalidStringProperty"].Errors[0].ErrorMessage);
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:33,代码来源:ModelValidationNodeTest.cs

示例9: Validate_PassesNullContainerInstanceIfCannotBeConvertedToProperType

        public void Validate_PassesNullContainerInstanceIfCannotBeConvertedToProperType() {
            // Arrange
            List<string> log1 = new List<string>();
            LoggingDataErrorInfoModel model1 = new LoggingDataErrorInfoModel(log1);
            ModelMetadata modelMetadata1 = GetModelMetadata(model1);

            List<string> log2 = new List<string>();
            LoggingDataErrorInfoModel model2 = new LoggingDataErrorInfoModel(log2);
            ModelMetadata modelMetadata2 = GetModelMetadata(model2);

            ControllerContext controllerContext = new ControllerContext() {
                Controller = new EmptyController()
            };
            ModelValidationNode node = new ModelValidationNode(modelMetadata1, "theKey");
            node.ChildNodes.Add(new ModelValidationNode(modelMetadata2, "theKey.SomeProperty"));

            // Act
            node.Validate(controllerContext);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "In IDataErrorInfo.get_Error()" },
                log1);

            CollectionAssert.AreEqual(
                new string[] { "In IDataErrorInfo.get_Error()" },
                log2);
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:28,代码来源:ModelValidationNodeTest.cs

示例10: Validate_Ordering

        public void Validate_Ordering() {
            // Proper order of invocation:
            // 1. OnValidating()
            // 2. Child validators
            // 3. This validator
            // 4. OnValidated()

            // Arrange
            List<string> log = new List<string>();
            LoggingDataErrorInfoModel model = new LoggingDataErrorInfoModel(log);
            ModelMetadata modelMetadata = GetModelMetadata(model);

            ControllerContext controllerContext = new ControllerContext() {
                Controller = new EmptyController()
            };
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey");

            ModelMetadata childMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(() => model, model.GetType(), "ValidStringProperty");
            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.ValidStringProperty"));

            node.Validating += delegate {
                log.Add("In OnValidating()");
            };
            node.Validated += delegate {
                log.Add("In OnValidated()");
            };

            // Act
            node.Validate(controllerContext);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "In OnValidating()", "In IDataErrorInfo.get_Item('ValidStringProperty')", "In IDataErrorInfo.get_Error()", "In OnValidated()" },
                log);
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:35,代码来源:ModelValidationNodeTest.cs

示例11: CombineWith_OtherNodeIsSuppressed_DoesNothing

        public void CombineWith_OtherNodeIsSuppressed_DoesNothing()
        {
            // Arrange
            List<string> log = new List<string>();

            ModelValidationNode[] allChildNodes = new[]
            {
                new ModelValidationNode(GetModelMetadata(), "key1"),
                new ModelValidationNode(GetModelMetadata(), "key2"),
                new ModelValidationNode(GetModelMetadata(), "key3"),
            };

            ModelValidationNode[] expectedChildNodes = new[]
            {
                allChildNodes[0]
            };

            ModelValidationNode parentNode1 = new ModelValidationNode(GetModelMetadata(), "parent1");
            parentNode1.ChildNodes.Add(allChildNodes[0]);
            parentNode1.Validating += delegate { log.Add("Validating parent1."); };
            parentNode1.Validated += delegate { log.Add("Validated parent1."); };

            ModelValidationNode parentNode2 = new ModelValidationNode(GetModelMetadata(), "parent2");
            parentNode2.ChildNodes.Add(allChildNodes[1]);
            parentNode2.ChildNodes.Add(allChildNodes[2]);
            parentNode2.Validating += delegate { log.Add("Validating parent2."); };
            parentNode2.Validated += delegate { log.Add("Validated parent2."); };
            parentNode2.SuppressValidation = true;

            // Act
            parentNode1.CombineWith(parentNode2);
            parentNode1.Validate(new ControllerContext { Controller = new EmptyController() });

            // Assert
            Assert.Equal(new[] { "Validating parent1.", "Validated parent1." }, log.ToArray());
            Assert.Equal(expectedChildNodes, parentNode1.ChildNodes.ToArray());
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:37,代码来源:ModelValidationNodeTest.cs


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