0

我正在做客戶端使用jQuery的自定義驗證,我堅持從 獲取值從服務器端執行操作在客戶端.... 這是我的服務器端自定義驗證函數無法獲取客戶端自定義驗證功能的值

public class SelctedValueCheckAttribute : ValidationAttribute , IClientValidatable 
{ 
    public SelctedValueCheckAttribute(string otherProperty): base("{0} is not in correct range") 
    { 
     OtherProperty = otherProperty; 
    } 
    public string OtherProperty { get; set; } 
    public SelctedValueCheckAttribute() 
    { 
     ErrorMessage = "Values must be in the Given Range"; 
    } 

    public override string FormatErrorMessage(string name) 
    { 
     return "The Entered Value Must be in given range for " + name + "item"; 
    } 
    protected override ValidationResult IsValid(object firstValue, ValidationContext validationContext) 
    { 
     string selecetdItemValue = firstValue as string ; 
     string userEnteredValue = GetSecondValue(validationContext); 
     if(string.Equals(selecetdItemValue, "Amount")) 
     { 
      int entry = Convert.ToInt32(userEnteredValue); 
      if (entry < 10 || entry > 20) 
      { 
       return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); 
      }         
     } 
     else if (string.Equals(selecetdItemValue, "Pound")) 
     { 
      int entry = Convert.ToInt32(userEnteredValue); 
      if (entry < 80 || entry > 90) 
      { 
       return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); 
      } 
     } 
     else if (string.Equals(selecetdItemValue, "Percent")) 
     { 
      int entry = Convert.ToInt32(userEnteredValue); 
      if (entry < 50 || entry > 60) 
      { 
       return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); 
      } 
     } 
     return ValidationResult.Success; 
    } 
    protected string GetSecondValue(ValidationContext validationContext) 
    { 
     var propertyInfo = validationContext.ObjectType.GetProperty(OtherProperty); 
     if (propertyInfo != null) 
     { 
     var secondValue = propertyInfo.GetValue(validationContext.ObjectInstance, null); 
     return secondValue as string; 
     } 
     return null; 
    }    
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     ModelClientValidationRule mcvr = new ModelClientValidationRule(); 
     mcvr.ValidationType = "enteredvaluescheck"; 
     mcvr.ErrorMessage = "Selected Value must be in given range"; 
     mcvr.ValidationParameters.Add("other", OtherProperty); 
     mcvr.ValidationType = "selectedvaluewithenteredvaluecheck"; 
     yield return mcvr; 
    } 
} 

,這是我的客戶端自定義驗證

  jquery.validator.unobtrusive.adapters.addSingleval("selectedvaluewithenteredvaluecheck", "other"); 
jQuery.validator.addMethod("selectedvaluewithenteredvaluecheck", 
          function(val,element,other) 
          { 
           if(val & other) 
           { 
           // here I am not getting the values.. 
           //do I need to write any function to get the values 
           //is there any other approach that I need to follow 
           } 

我怎樣才能在客戶端函數值是多少? 任何人有任何關於此的想法和任何建議,請建議我。

+0

如果這個問題不清楚,你能否告訴我... –

+0

請看這裏。 Theres在客戶端做些什麼:http://www.codeproject.com/Articles/301022/Creating-Custom-Validation-Attribute-in-MVC-3 – Fals

回答

0

你錯過了拼寫jqueryaddSingleval。他們應該大寫QV

jQuery.validator.unobtrusive.adapters.addSingleVal("selectedvaluewithenteredvaluecheck", "other"); 
+0

謝謝,但我正在尋找獲得價值的'ADDmethod' ... –

+0

沒有修復代碼,您將無法看到裏面的數據。 JavaScript區分大小寫。讓我們逐個分離問題。 – zsong

+0

謝謝..我已經這樣做了,但我不知道如何檢查此功能(客戶端自定義驗證)是否工作....你會告訴我如何檢查.. –

相關問題