从自定义验证属性中访问ErrorMessage

时间:2013-02-13 16:15:22

标签: asp.net-mvc-3 unobtrusive-validation validationattribute

我希望能够读取传递给自定义验证属性的ErrorMessage字符串,以便我可以对其进行string.Format()。但我似乎无法找到它?

例如,我有一个自定义属性:

[IsDateBeforeFixedDate(4, 0, 0, ErrorMessage = "*The departure date should be between 4 days and 11 months")]

decalred as:

  public sealed class IsDateBeforeFixedDateAttribute : ValidationAttribute, IClientValidatable
  {

    public IsDateBeforeFixedDateAttribute(int days, int months, int years) : base(days, months, years)
    {
      //I want to read the ErrorMessage string here! (i.e. "*The departure date should be between 4 days and 11 months")
    }
  }

ErrorMessageString不包含它或ErrorMessage

1 个答案:

答案 0 :(得分:0)

似乎如果我在IsValid方法中访问它,它包含正确的字符串,但它不在构造函数中