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


C# DataAnnotationsModelMetadataProvider.GetMetadataForProperty方法代码示例

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


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

示例1: DataAnnotationsModelMetadataProvider_ReadsScaffoldColumnAttribute_ForShowForEdit

        public void DataAnnotationsModelMetadataProvider_ReadsScaffoldColumnAttribute_ForShowForEdit()
        {
            // Arrange
            var type = typeof(ScaffoldColumnModel);
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            Assert.True(provider.GetMetadataForProperty(null, type, "NoAttribute").ShowForEdit);
            Assert.True(provider.GetMetadataForProperty(null, type, "ScaffoldColumnTrue").ShowForEdit);
            Assert.False(provider.GetMetadataForProperty(null, type, "ScaffoldColumnFalse").ShowForEdit);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:11,代码来源:CachedDataAnnotationsModelMetadataProviderTest.cs

示例2: ReadOnlyTests

        public void ReadOnlyTests()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            Assert.False(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "NoAttributes").IsReadOnly);
            Assert.True(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "ReadOnlyAttribute").IsReadOnly);
            Assert.True(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "EditableAttribute").IsReadOnly);
            Assert.False(provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), "BothAttributes").IsReadOnly);
        }
开发者ID:kiran2013,项目名称:aspnetwebstack,代码行数:11,代码来源:DataAnnotationsModelMetadataProviderTest.cs

示例3: HiddenAttributeSetsTemplateHintAndHideSurroundingHtml

        public void HiddenAttributeSetsTemplateHintAndHideSurroundingHtml() {
            // Arrange
            DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            ModelMetadata noAttributeMetadata = provider.GetMetadataForProperty(null, typeof(HiddenModel), "NoAttribute");
            Assert.IsNull(noAttributeMetadata.TemplateHint);
            Assert.IsFalse(noAttributeMetadata.HideSurroundingHtml);

            ModelMetadata defaultHiddenMetadata = provider.GetMetadataForProperty(null, typeof(HiddenModel), "DefaultHidden");
            Assert.AreEqual("HiddenInput", defaultHiddenMetadata.TemplateHint);
            Assert.IsFalse(defaultHiddenMetadata.HideSurroundingHtml);

            ModelMetadata hiddenWithDisplayValueFalseMetadata = provider.GetMetadataForProperty(null, typeof(HiddenModel), "HiddenWithDisplayValueFalse");
            Assert.AreEqual("HiddenInput", hiddenWithDisplayValueFalseMetadata.TemplateHint);
            Assert.IsTrue(hiddenWithDisplayValueFalseMetadata.HideSurroundingHtml);

            // [UIHint] overrides the template hint from [Hidden]
            Assert.AreEqual("CustomUIHint", provider.GetMetadataForProperty(null, typeof(HiddenModel), "HiddenAndUIHint").TemplateHint);
        }
开发者ID:consumentor,项目名称:Server,代码行数:20,代码来源:DataAnnotationsModelMetadataProviderTest.cs

示例4: ReadOnlyTests

        public void ReadOnlyTests(string propertyName, bool expected)
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var actual = provider.GetMetadataForProperty(null, typeof(ReadOnlyModel), propertyName).IsReadOnly;

            // Assert
            Assert.Equal(expected, actual);
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:11,代码来源:DataAnnotationsModelMetadataProviderTest.cs

示例5: GetMetadataForPropertySetsTypeAndPropertyName

        public void GetMetadataForPropertySetsTypeAndPropertyName() {
            // Arrange
            DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();

            // Act
            ModelMetadata result = provider.GetMetadataForProperty(null, typeof(string), "Length");

            // Assert
            Assert.AreEqual(typeof(int), result.ModelType);
            Assert.AreEqual("Length", result.PropertyName);
        }
开发者ID:consumentor,项目名称:Server,代码行数:11,代码来源:DataAnnotationsModelMetadataProviderTest.cs

示例6: ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName

        public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName()
        {
            // Arrange
            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");
            var attribute = new CompareAttribute("OtherProperty");
            var context = new ClientModelValidationContext(metadata, metadataProvider);
            var adapter = new CompareAttributeAdapter(attribute);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:16,代码来源:CompareAttributeTest.cs

示例7: GetClientValidationRules_ReturnsValidationParameters

        public void GetClientValidationRules_ReturnsValidationParameters()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();
            var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
            var attribute = new RequiredAttribute();
            var adapter = new RequiredAttributeAdapter(attribute);
            var context = new ClientModelValidationContext(metadata, provider);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("required", rule.ValidationType);
            Assert.Empty(rule.ValidationParameters);
            Assert.Equal("The Length field is required.", rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:18,代码来源:RequiredAttributeAdapterTest.cs

示例8: ClientRulesWithMaxLengthAttribute

        public void ClientRulesWithMaxLengthAttribute()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();
            var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
            var attribute = new MaxLengthAttribute(10);
            var adapter = new MaxLengthAttributeAdapter(attribute);
            var context = new ClientModelValidationContext(metadata, provider);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("maxlength", rule.ValidationType);
            Assert.Equal(1, rule.ValidationParameters.Count);
            Assert.Equal(10, rule.ValidationParameters["max"]);
            Assert.Equal("The field Length must be a string or array type with a maximum length of '10'.", rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:19,代码来源:MaxLengthAttributeAdapterTest.cs

示例9: GetClientValidationRules_ReturnsValidationParameters

        public void GetClientValidationRules_ReturnsValidationParameters()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();
            var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
            var attribute = new RangeAttribute(typeof(decimal), "0", "100");
            var adapter = new RangeAttributeAdapter(attribute);
            var context = new ClientModelValidationContext(metadata, provider);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("range", rule.ValidationType);
            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal(0m, rule.ValidationParameters["min"]);
            Assert.Equal(100m, rule.ValidationParameters["max"]);
            Assert.Equal(@"The field Length must be between 0 and 100.", rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:20,代码来源:RangeAttributeAdapterTest.cs

示例10: ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride

        public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride()
        {
            // Arrange
            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");
            var attribute = new CompareAttribute("OtherProperty")
            {
                ErrorMessageResourceName = "CompareAttributeTestResource",
                ErrorMessageResourceType = typeof(Test.Resources),
            };
            var context = new ClientModelValidationContext(metadata, metadataProvider);
            var adapter = new CompareAttributeAdapter(attribute);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("Comparing MyProperty to OtherProperty.", rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:20,代码来源:CompareAttributeTest.cs

示例11: GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParameters

        public void GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParameters()
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();
            var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
            var attribute = new StringLengthAttribute(10) { MinimumLength = 3 };
            var adapter = new StringLengthAttributeAdapter(attribute);
            var context = new ClientModelValidationContext(metadata, provider);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("length", rule.ValidationType);
            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal(3, rule.ValidationParameters["min"]);
            Assert.Equal(10, rule.ValidationParameters["max"]);
            Assert.Equal("The field Length must be a string with a minimum length of 3 and a maximum length of 10.",
                         rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:21,代码来源:StringLengthAttributeAdapterTest.cs

示例12: ClientRulesWithMaxLengthAttributeAndCustomMessage

        public void ClientRulesWithMaxLengthAttributeAndCustomMessage()
        {
            // Arrange
            var propertyName = "Length";
            var message = "{0} must be at most {1}";
            var provider = new DataAnnotationsModelMetadataProvider();
            var metadata = provider.GetMetadataForProperty(() => null, typeof(string), propertyName);
            var attribute = new MaxLengthAttribute(5) { ErrorMessage = message };
            var adapter = new MaxLengthAttributeAdapter(attribute);
            var context = new ClientModelValidationContext(metadata, provider);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);
            Assert.Equal("maxlength", rule.ValidationType);
            Assert.Equal(1, rule.ValidationParameters.Count);
            Assert.Equal(5, rule.ValidationParameters["max"]);
            Assert.Equal("Length must be at most 5", rule.ErrorMessage);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:21,代码来源:MaxLengthAttributeAdapterTest.cs

示例13: DataAnnotationsNameTests

        [InlineData("DisplayNameDefault", "")] // The default for DisplayName is the empty string, we don't have special handling for it, and nither does MVC.
        public void DataAnnotationsNameTests(string propertyName, string expected)
        {
            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var actual = provider.GetMetadataForProperty(null, typeof(DisplayModel), propertyName).GetDisplayName();

            // Assert
            Assert.Equal(expected, actual);
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:12,代码来源:DataAnnotationsModelMetadataProviderTest.cs

示例14: DisplayAttribute_WithLocalizedName

        public void DisplayAttribute_WithLocalizedName()
        {
            // Guard
            var expected = Resources.String1;
            Assert.NotEqual("String1", expected);

            // Arrange
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var actual = provider.GetMetadataForProperty(null, typeof(DisplayModel), "Localized").GetDisplayName();
            
            // Assert
            Assert.Equal(expected, actual);
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:15,代码来源:DataAnnotationsModelMetadataProviderTest.cs

示例15: IsRequiredTests

        public void IsRequiredTests() {
            // Arrange
            DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();

            // Act & Assert
            Assert.IsTrue(provider.GetMetadataForProperty(null, typeof(IsRequiredModel), "NonNullableWithout").IsRequired);
            Assert.IsFalse(provider.GetMetadataForProperty(null, typeof(IsRequiredModel), "NullableWithout").IsRequired);
            Assert.IsTrue(provider.GetMetadataForProperty(null, typeof(IsRequiredModel), "NullableWith").IsRequired);
        }
开发者ID:consumentor,项目名称:Server,代码行数:9,代码来源:DataAnnotationsModelMetadataProviderTest.cs


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