本文整理汇总了C#中DataAnnotationsModelMetadataProvider类的典型用法代码示例。如果您正苦于以下问题:C# DataAnnotationsModelMetadataProvider类的具体用法?C# DataAnnotationsModelMetadataProvider怎么用?C# DataAnnotationsModelMetadataProvider使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DataAnnotationsModelMetadataProvider类属于命名空间,在下文中一共展示了DataAnnotationsModelMetadataProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例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"]);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}