本文整理汇总了C#中System.Web.Http.Metadata.ModelMetadata.GetDisplayName方法的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadata.GetDisplayName方法的具体用法?C# ModelMetadata.GetDisplayName怎么用?C# ModelMetadata.GetDisplayName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Http.Metadata.ModelMetadata
的用法示例。
在下文中一共展示了ModelMetadata.GetDisplayName方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
// Per the WCF RIA Services team, instance can never be null (if you have
// no parent, you pass yourself for the "instance" parameter).
ValidationContext context = new ValidationContext(container ?? metadata.Model, null, null);
context.DisplayName = metadata.GetDisplayName();
ValidationResult result = Attribute.GetValidationResult(metadata.Model, context);
if (result != ValidationResult.Success)
{
return new ModelValidationResult[] { new ModelValidationResult { Message = result.ErrorMessage } };
}
return new ModelValidationResult[0];
}
示例2: Validate
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
// Per the WCF RIA Services team, instance can never be null (if you have
// no parent, you pass yourself for the "instance" parameter).
string memberName = metadata.GetDisplayName();
ValidationContext context = new ValidationContext(container ?? metadata.Model)
{
DisplayName = memberName,
MemberName = memberName
};
ValidationResult result = Attribute.GetValidationResult(metadata.Model, context);
if (result != ValidationResult.Success)
{
// ModelValidationResult.MemberName is used by invoking validators (such as ModelValidationNode) to
// construct the ModelKey for ModelStateDictionary. When validating at type level we want to append the
// returned MemberNames if specified (e.g. person.Address.FirstName). For property validation, the
// ModelKey can be constructed using the ModelMetadata and we should ignore MemberName (we don't want
// (person.Name.Name). However the invoking validator does not have a way to distinguish between these two
// cases. Consequently we'll only set MemberName if this validation returns a MemberName that is different
// from the property being validated.
string errorMemberName = result.MemberNames.FirstOrDefault();
if (String.Equals(errorMemberName, memberName, StringComparison.Ordinal))
{
errorMemberName = null;
}
var validationResult = new ModelValidationResult
{
Message = result.ErrorMessage,
MemberName = errorMemberName
};
return new ModelValidationResult[] { validationResult };
}
return Enumerable.Empty<ModelValidationResult>();
}
示例3: GetResourceCommon
private static string GetResourceCommon(HttpActionContext actionContext, ModelMetadata modelMetadata, object incomingValue, Func<HttpActionContext, string> resourceAccessor)
{
string displayName = modelMetadata.GetDisplayName();
string errorMessageTemplate = resourceAccessor(actionContext);
return Error.Format(errorMessageTemplate, incomingValue, displayName);
}
示例4: ReturnsTypeNameWhenPropertyNameAndDisplayNameAreNull
public void ReturnsTypeNameWhenPropertyNameAndDisplayNameAreNull()
{
// Arrange
Mock<ModelMetadataProvider> provider = new Mock<ModelMetadataProvider>();
ModelMetadata metadata = new ModelMetadata(provider.Object, null, null, typeof(object), null);
// Act
string result = metadata.GetDisplayName();
// Assert
Assert.Equal("Object", result);
}
示例5: ReturnsDisplayNameWhenSet
public void ReturnsDisplayNameWhenSet()
{
// Arrange
Mock<ModelMetadataProvider> provider = new Mock<ModelMetadataProvider>();
ModelMetadata metadata = new ModelMetadata(provider.Object, null, null, typeof(object), "PropertyName") { DisplayName = "Display Name" };
// Act
string result = metadata.GetDisplayName();
// Assert
Assert.Equal("Display Name", result);
}