如何更改属性错误消息

时间:2011-09-07 07:16:46

标签: asp.net-mvc-2

我重写了RequiredAttribute,在我的IsValid方法中,我需要能够更改ErrorMessage属性。这可能吗?

1 个答案:

答案 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);
    }
}