C# 如何将DisplayName设置为ErrorMessage格式
我有这样的想法:C# 如何将DisplayName设置为ErrorMessage格式,c#,asp.net-mvc,data-annotations,C#,Asp.net Mvc,Data Annotations,我有这样的想法: [DisplayName("First Name")] [Required(ErrorMessage="{0} is required.")] [StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")] public string Name { get; set; } public class Require
[DisplayName("First Name")]
[Required(ErrorMessage="{0} is required.")]
[StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
public string Name { get; set; }
public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
private String displayName;
public RequiredAttribute()
{
this.ErrorMessage = "{0} is required";
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var attributes = validationContext.ObjectType.GetProperty(validationContext.MemberName).GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (attributes != null)
this.displayName = (attributes[0] as DisplayNameAttribute).DisplayName;
else
this.displayName = validationContext.DisplayName;
return base.IsValid(value, validationContext);
}
public override string FormatErrorMessage(string name)
{
return string.Format(this.ErrorMessageString, displayName);
}
}
我希望有以下输出:
- 名字是必需的李>
- 名字的长度应该在10到50之间李>
ValidationContext context = new ValidationContext(myModel, null, null);
List<ValidationResult> results = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(myModel, context, results, true);
ValidationContext=newvalidationcontext(myModel,null,null);
列表结果=新列表();
bool valid=Validator.TryValidateObject(myModel,context,results,true);
结果是:
- 名称是必需的李>
- 名称的长度应介于10和50之间李>
怎么了?谢谢。嗯,我想我做到了 我必须创建另一个属性,如下所示:
[DisplayName("First Name")]
[Required(ErrorMessage="{0} is required.")]
[StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
public string Name { get; set; }
public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
private String displayName;
public RequiredAttribute()
{
this.ErrorMessage = "{0} is required";
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var attributes = validationContext.ObjectType.GetProperty(validationContext.MemberName).GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (attributes != null)
this.displayName = (attributes[0] as DisplayNameAttribute).DisplayName;
else
this.displayName = validationContext.DisplayName;
return base.IsValid(value, validationContext);
}
public override string FormatErrorMessage(string name)
{
return string.Format(this.ErrorMessageString, displayName);
}
}
我的模型是:
[DisplayName("Full name")]
[Required]
public string Name { get; set; }
谢天谢地,此DataAnnotation是可扩展的。使用[DisplayName]
属性,而不是(或者与之结合)使用[Display]
属性,在System.ComponentModel.DataAnnotations
中使用[Display]
属性。填充其名称
属性
这样,您就可以在ValidationContext
的DisplayName
中使用内置的验证属性或自定义属性
e、 g
[Display(Name=“First Name”)]//这确实会更改所需标记的呈现方式。当我有更多的时间来验证entity framework是否满足这个问题的初始需求时,我将尝试使用一个不会影响entity framework如何进行客户端验证的解决方案。我似乎与OP有相同的问题。大体上,在属性上使用[DisplayName]或[Display]都可以正常工作(替换{0}但是,我发现服务器端验证输出的模型错误可能是不正确的(用属性名替换{0}),即使不引人注目的验证为同一属性提供了正确的值。(要看到这一点,我必须关闭javascript)。我还没有找到这个问题的根源-用MVC 5.2.3观察到的非常好。。。你救了我一天。请问你是从哪里得到这个信息的?对不起,我不记得了。再次罢工。尽管如此,我可以非常自信地说,我并没有从MSDN文档中阅读它。