我重写了RequiredAttribute,在我的IsValid方法中,我需要能够更改ErrorMessage属性。这可能吗?
答案 0 :(得分:8)
[Required(ErrorMessage = "Some custom required message about the Foo property")]
public string Foo { get; set; }
并在IsValid方法中:
public class MyRequiredAttribute : RequiredAttribute
{
public override bool IsValid(object value)
{
ErrorMessage = "Some custom required message";
...
return base.IsValid(value);
}
}