C# 无法在客户端自定义验证函数中获取值
我正在使用jQuery进行客户端自定义验证,我一直在从中获取值 服务器端在客户端执行操作。。。。 这是我的服务器端自定义验证功能C# 无法在客户端自定义验证函数中获取值,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,unobtrusive-validation,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Unobtrusive Validation,我正在使用jQuery进行客户端自定义验证,我一直在从中获取值 服务器端在客户端执行操作。。。。 这是我的服务器端自定义验证功能 public class SelctedValueCheckAttribute : ValidationAttribute , IClientValidatable { public SelctedValueCheckAttribute(string otherProperty): base("{0} is not in correct range")
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
和addSingleval
。它们应该大写Q
和V
jQuery.validator.unobtrusive.adapters.addSingleVal("selectedvaluewithenteredvaluecheck", "other");
如果不清楚,请告诉我……看看这里。在客户端有一些事情要做:如果不修改代码,您将无法看到其中的数据。Javascript区分大小写。让我们把问题一个一个地孤立起来。谢谢。。我已经这样做了,但我不知道如何检查此函数(客户端自定义验证)是否正常工作…请告诉我如何检查..在
函数(val,element,other)'中,您可以尝试控制台.log(val)`查看是否将在浏览器控制台中打印。当我在internet explorer上运行0x800a138f中第1列第2行的未处理异常时,我遇到此错误-Microsoft JScript运行时错误:“jQuery.validator.unobtrusive”为空或不是我在单独的js文件中编写客户端验证的对象。。。
jQuery.validator.unobtrusive.adapters.addSingleVal("selectedvaluewithenteredvaluecheck", "other");