-1

我有一個MVC應用程序,我添加了自定義的跨場驗證。交叉字段驗證未配置爲客戶端,但是當我通過我的字段選項卡IE瀏覽器拋出以下錯誤「$ .validator.method [...]爲null或不是一個對象」從jquery.validate .js文件。我附上完整的版本,所以我可以調試這是怎麼回事,它似乎是爲了要低於解僱我的「mandatoryif」自定義驗證的客戶端,然後在下面的行引發錯誤:IE中的jQuery.validate錯誤

var result = $.validator.methods[method].call(this, element.value.replace(/\r/g, ""), element, rule.parameters); 

任何想法爲什麼當「mandatoryif」驗證沒有被添加到客戶端時它試圖做到這一點?

我也更新到最新版本,因爲我讀它可能是jQuery.validate的版本,但這並沒有解決問題。

這裏是我的自定義驗證: 屬性

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] 
public class MandatoryIfAttribute : ValidationAttribute, ICrossFieldValidationAttribute 
{  
    public string OtherProperty { get; set; } 

    public bool IsValid(ControllerContext controllerContext, object model, ModelMetadata modelMetadata) 
    { 
     if (model == null) 
     { 
      throw new ArgumentNullException("model"); 
     } 

     // Find the value of the other property. 
     var propertyInfo = model.GetType().GetProperty(OtherProperty); 
     if (propertyInfo == null) 
     { 
      throw new InvalidOperationException(string.Format("Couldn't find {0} property on {1}.", 
                   OtherProperty, model)); 
     } 

     var otherValue = propertyInfo.GetGetMethod().Invoke(model, null); 

     if (modelMetadata.Model == null) 
     { 
      modelMetadata.Model = string.Empty; 
     } 

     if (otherValue == null) 
     { 
      otherValue = string.Empty; 
     } 

     return (String.IsNullOrEmpty(modelMetadata.Model.ToString()) && (String.IsNullOrEmpty(otherValue.ToString()) || otherValue.ToString() == "0")) || (!String.IsNullOrEmpty(modelMetadata.Model.ToString()) && String.IsNullOrEmpty(otherValue.ToString())) || (!String.IsNullOrEmpty(modelMetadata.Model.ToString()) && !String.IsNullOrEmpty(otherValue.ToString())); 
    } 

    public override bool IsValid(object value) 
    { 
     // Work done in other IsValid 
     return true; 
    } 

驗證

public class MandatoryIfValidator : CrossFieldValidator<MandatoryIfAttribute> 
{  
    public MandatoryIfValidator(ModelMetadata metadata, ControllerContext controllerContext, 
            MandatoryIfAttribute attribute) : 
     base(metadata, controllerContext, attribute) 
    { 
    } 

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() 
    { 
     var rule = new ModelClientValidationRule 
     { 
      ValidationType = "mandatoryif", 
      ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName), 
     }; 

     rule.ValidationParameters.Add("otherProperty", Attribute.OtherProperty); 

     return new[] { rule }; 
    } 

欣賞任何幫助。

+1

機會很高,這個問題將保持當前狀態(未答覆),而不顯示您的代碼。編號: – 2011-05-19 08:37:20

+0

Bueller,bueller。 – doogdeb 2011-05-20 10:30:20

回答

1

我已經設法找到我的查詢的答案。因爲我的一些自定義驗證正在對db進行查找,所以我不想讓這些客戶端的開銷降到最低,所以沒有客戶端代碼。但是,我並沒有在Global.asax中添加

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MandatoryIfAttribute), typeof(MandatoryIfValidator));

它在影響增加這個客戶端意識到這一點。當然,因爲沒有處理客戶端的代碼,所以它拋出了validaiton錯誤。