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

谢谢..我已经这样做了,但我不知道如何检查此功能(客户端自定义验证)是否工作....你会告诉我如何检查.. –

相关问题