此类尚未实现IsValid(对象值)

cho*_*bo2 6 c# asp.net-mvc data-annotations asp.net-mvc-3

我试图使用asp.net mvc 3无障碍javascript与jquery.

我正在关注本教程

我不清楚如何做第一步.

我认为它只是覆盖了IsValid,但我一直收到错误所以我一定做错了

 public class EmailAttribute : ValidationAttribute, IClientValidatable
        {

            public override bool IsValid(object value)
            {
                return base.IsValid(value);
            }

            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                yield return new ModelClientValidationEmailRule(FormatErrorMessage(metadata.GetDisplayName()));
            }
        }

       public class ModelClientValidationEmailRule : ModelClientValidationRule
        {
            public ModelClientValidationEmailRule(string errorMessage)
            {
                base.ErrorMessage = errorMessage;
                base.ValidationType = "email";
            }
        }
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

IsValid(object value) has not been implemented by this class.  The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotImplementedException: IsValid(object value) has not been implemented by this class.  The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).

Source Error:

Line 13:         public override bool IsValid(object value)
Line 14:         {
Line 15:             return base.IsValid(value);
Line 16:         }
Line 17: 
Run Code Online (Sandbox Code Playgroud)

Pet*_*sen 6

IsValid方法应包含验证逻辑.

您只是调用IsValid的基类实现,它似乎抛出了NotImplementedException,因为它应该在子类中被覆盖.

public override bool IsValid(object value)
{
    //return true if 'value' is a valid email. Otherwise return false.
}
Run Code Online (Sandbox Code Playgroud)

  • 仍然想知道为什么他们不将 IsValid 设为抽象方法。谁能解释一下吗? (2认同)