【问题标题】:client side validation in MVC3 not workingMVC3 中的客户端验证不起作用
【发布时间】:2012-04-25 07:17:02
【问题描述】:

以下是客户端验证不起作用的代码...我在这个论坛中搜索了几个问题并写了这个..

这里是自定义验证属性“startDateAttribute”

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class StartDateAttribute : ValidationAttribute, IClientValidatable
    {
        public StartDateAttribute ()
        {
        }

        public override bool IsValid(object value)
        {   
            var date = (DateTime)value;
            if (date.Date >= DateTime.Now.Date)
            {
                return true;
            }
            return false;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientValidationRule
            {
                ErrorMessage = this.ErrorMessage,
                ValidationType = "DateRange"
            };
        }
    }

    [CurrentDateAttribute(ErrorMessage = "select the correct date")]    
    public DateTime? StartDate { get; set; }

这里是添加的 JQuery 代码

      jQuery.validator.addMethod('DateRange', function (value, element, params) {
     var d = new Date();         
     var currentDate = (d.getMonth()+1)  + "/"+d.getDate()+ "/" + d.getFullYear() ;
    return value >= currentDate;
});

// and an unobtrusive adapter
jQuery.validator.unobtrusive.adapters.add('DateRange', { }, function (options) {
    options.rules['DateRange'] = true;
    options.messages['DateRange'] = options.message;
});

【问题讨论】:

    标签: asp.net-mvc-3 custom-attributes client-side-validation


    【解决方案1】:

    客户端验证的要求之一是 ValidationType 和适配器名称应该匹配并且应该是小写的。

    将 ValidationType 和适配器名称更改为 'daterange' 并检查

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多