【问题标题】:unable to get property value of undefined or null reference无法获取未定义或空引用的属性值
【发布时间】:2014-06-23 09:37:17
【问题描述】:

我正在使用下面的代码来验证输入的交货日期不应小于今天的日期..为此我在下面做了这样的......

我正在使用 jquery 进行自定义验证...

这是我的模型:

    [UIHint("Date")]
    [DeliveryDateCheck]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    [Required(ErrorMessage="Required")]
    public DateTime? DeliveryDate { set; get; }

这是服务器端代码验证

public class DeliveryDateCheck : ValidationAttribute, IClientValidatable
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string errorMessage = "Delivery date should be today date or Greater";
        if(value != null)
        {
            DateTime dt = (DateTime)value;
            if (dt.Date < DateTime.Now.Date)
            {
                return new ValidationResult(errorMessage);
            }
        }            
        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ModelClientValidationRule mcvRule = new ModelClientValidationRule();
        mcvRule.ValidationType = "checkdate";
        mcvRule.ErrorMessage = "Delivery date should be today date or Greater";
        yield return mcvRule;  
    }
}

这是客户端验证

  $(document).ready(function () {

    jQuery.validator.unobtrusive.adapters.add('checkdate', {}, function (options) {
        options.rules['checkdate'] = true;
        options.messages['checkdate'] = options.message;
    });

    jQuery.validator.addMethod('checkdate', function (value, element, params) {
        if (value) {
            var todayDate = new Date();
            var compareDate = value.Date;
            if (compareDate < todayDate) {
                return false;
            }
        }
        return true;
    });

});

但我收到这样的错误...

错误

 0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference

我可以看到服务器端验证工作正常,但无法进行客户端验证 有人知道为什么我在这里出错.....

非常感谢提前

【问题讨论】:

  • 应该是var compareDate = value; 而不是var compareDate = value.Date; 吗?
  • @Bellash 我改变了它,但我仍然得到同样的错误......

标签: c# javascript jquery asp.net-mvc validation


【解决方案1】:

不应该比较日期如下:

var compareDate = new Date(value);

【讨论】:

    【解决方案2】:
      $(document).ready(function () {
    
    jQuery.validator.unobtrusive.adapters.add('checkdate', {}, function (options) {
        options.rules['checkdate'] = true;
        options.messages['checkdate'] = options.message;
    });
    
    jQuery.validator.addMethod('checkdate', function (value, element, params) {
        if (value) {
            var todayDate = new Date();
            var compareDate = new Date(value);
            if (compareDate < todayDate) {
                return false;
            }
        }
        return true;
        });
     });
    

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 2016-03-29
      • 1970-01-01
      • 2014-08-01
      • 2020-04-28
      • 2016-08-16
      • 2017-05-18
      • 2014-08-29
      • 1970-01-01
      相关资源
      最近更新 更多