本文整理匯總了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);
}