• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# DataAnnotationsModelMetadataProvider类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: 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


示例2: GetMetadataForTypeSetsTypeWithNullPropertyName

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

            // Act
            ModelMetadata result = provider.GetMetadataForType(null, typeof(string));

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


示例3: 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


示例4: ReadOnlyTests

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

            IModelFlattener test = new DefaultModelFlattener();
            var model = new DisplayModel();
            var result = test.Flatten(model, model.GetType(), provider, string.Empty);

            Assert.NotNull(result);
        }
开发者ID:ctguxp,项目名称:Waffle,代码行数:11,代码来源:DataAnnotationsModelMetadataProviderTests.cs


示例5: 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


示例6: 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


示例7: GetMetadataForPropertiesSetTypesAndPropertyNames

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

            // Act
            IEnumerable<ModelMetadata> result = provider.GetMetadataForProperties("foo", typeof(string));

            // Assert
            Assert.IsTrue(result.Any(m => m.ModelType == typeof(int)
                                          && m.PropertyName == "Length"
                                          && (int)m.Model == 3));
        }
开发者ID:consumentor,项目名称:Server,代码行数:12,代码来源:DataAnnotationsModelMetadataProviderTest.cs


示例8: 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


示例9: 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


示例10: 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


示例11: SettingViewData_AlsoUpdatesViewBag

        public void SettingViewData_AlsoUpdatesViewBag()
        {
            // Arrange
            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var controller = new Controller();
            var originalViewData = controller.ViewData = new ViewDataDictionary<object>(metadataProvider);
            var replacementViewData = new ViewDataDictionary<object>(metadataProvider);

            // Act
            controller.ViewBag.Hello = "goodbye";
            controller.ViewData = replacementViewData;
            controller.ViewBag.Another = "property";

            // Assert
            Assert.NotSame(originalViewData, controller.ViewData);
            Assert.Same(replacementViewData, controller.ViewData);
            Assert.Null(controller.ViewBag.Hello);
            Assert.Equal("property", controller.ViewBag.Another);
            Assert.Equal("property", controller.ViewData["Another"]);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:20,代码来源:ControllerTests.cs


示例12: 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


示例13: 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


示例14: 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


示例15: 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


示例16: AttributesOverrideMetadataStrings

        public void AttributesOverrideMetadataStrings(Attribute attribute, Func<ModelMetadata, string> accessor)
        {
            // Arrange
            var attributes = new[] { attribute };
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(ClassWithDisplayableColumn),
                propertyName: null,
                attributes: attributes)
            {
                Model = new ClassWithDisplayableColumn { Property = "value" },
            };
            var result = accessor(metadata);

            // Assert
            Assert.Equal("value", result);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:21,代码来源:CachedDataAnnotationsModelMetadataTest.cs


示例17: 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


示例18: Constructor_DefersDefaultsToBaseModelMetadata

        public void Constructor_DefersDefaultsToBaseModelMetadata()
        {
            // Arrange
            var attributes = Enumerable.Empty<Attribute>();
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(object),
                propertyName: null,
                attributes: attributes);

            // Assert
            Assert.True(metadata.ConvertEmptyStringToNull);
            Assert.False(metadata.IsReadOnly);
            Assert.False(metadata.IsRequired);

            Assert.Null(metadata.Description);
            Assert.Null(metadata.DisplayName);
            Assert.Null(metadata.NullDisplayText);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:23,代码来源:CachedDataAnnotationsModelMetadataTest.cs


示例19: DataAnnotationsDescriptionTests

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

            // Act & Assert
            Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").Description);
            Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NothingSet").Description);
            Assert.Null(provider.GetMetadataForProperty(null, typeof(DisplayModel), "NameSet").Description);
            Assert.Equal("Description text1", provider.GetMetadataForProperty(null, typeof(DisplayModel), "DescriptionSet").Description);
            Assert.Equal("Description text2", provider.GetMetadataForProperty(null, typeof(DisplayModel), "BothSet").Description);
        }
开发者ID:kiran2013,项目名称:aspnetwebstack,代码行数:12,代码来源:DataAnnotationsModelMetadataProviderTest.cs


示例20: DataAnnotationsNameTests

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

            // Act & Assert
            Assert.Equal("NoAttribute", provider.GetMetadataForProperty(null, typeof(DisplayModel), "NoAttribute").GetDisplayName());
            Assert.Equal("NothingSet", provider.GetMetadataForProperty(null, typeof(DisplayModel), "NothingSet").GetDisplayName());
            Assert.Equal("", provider.GetMetadataForProperty(null, typeof(DisplayModel), "EmptyDisplayName").GetDisplayName());
            Assert.Equal("DescriptionSet", provider.GetMetadataForProperty(null, typeof(DisplayModel), "DescriptionSet").GetDisplayName());
            Assert.Equal("Name text1", provider.GetMetadataForProperty(null, typeof(DisplayModel), "NameSet").GetDisplayName());
            Assert.Equal("Name text2", provider.GetMetadataForProperty(null, typeof(DisplayModel), "BothSet").GetDisplayName());

            Assert.NotEqual("String1", Resources.String1);
            Assert.Equal(Resources.String1, provider.GetMetadataForProperty(null, typeof(DisplayModel), "Localized").GetDisplayName());
        }
开发者ID:kiran2013,项目名称:aspnetwebstack,代码行数:16,代码来源:DataAnnotationsModelMetadataProviderTest.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# DataBase类代码示例发布时间:2022-05-24
下一篇:
C# DataAccessLayer类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap