【问题标题】:IsValid(object value) has not been implemented by this class此类尚未实现 IsValid(object value)
【发布时间】:2011-01-30 01:18:12
【问题描述】:

我正在尝试将 asp.net mvc 3 unobstructed javascript 与 jquery 一起使用。

我正在关注这个Tutorial

我不清楚第一步该怎么做。

我以为它只是覆盖 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";
            }
        }

我收到此错误

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: 

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 data-annotations


    【解决方案1】:

    IsValid 方法应包含您的验证逻辑。

    您只是调用了 IsValid 的基类实现,这似乎是在抛出 NotImplementedException,因为它预计会在子类中被覆盖。

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

    【讨论】:

    • 仍然想知道他们为什么不将 IsValid 设为抽象方法。谁能解释一下?
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2012-05-18
    • 2019-12-13
    • 2021-11-29
    相关资源
    最近更新 更多