Asp.net mvc Can';t在.NET MVC客户端验证上访问ValidationParameters
我正在为.NETMVC4应用程序编写自定义验证代码。这是第一次使用参数进行验证,我发现要得到它有些麻烦 这是我的C#代码: 验证属性:Asp.net mvc Can';t在.NET MVC客户端验证上访问ValidationParameters,asp.net-mvc,validation,validationattribute,validationrule,Asp.net Mvc,Validation,Validationattribute,Validationrule,我正在为.NETMVC4应用程序编写自定义验证代码。这是第一次使用参数进行验证,我发现要得到它有些麻烦 这是我的C#代码: 验证属性: namespace Validations { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class MinRequiredPasswordLengthVa
namespace Validations
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class MinRequiredPasswordLengthValidationAttribute : ValidationAttribute, IClientValidatable
{
public MinRequiredPasswordLengthValidationAttribute()
: base("The password is invalid")
{
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name.ToLower(), Membership.MinRequiredPasswordLength);
}
public override bool IsValid(object value)
{
if (value == null || String.IsNullOrEmpty(value.ToString()))
{
return true;
}
return value.ToString().Length >= Membership.MinRequiredPasswordLength;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientMinRequiredPasswordLengthValidationRule rule = new ModelClientMinRequiredPasswordLengthValidationRule(FormatErrorMessage(metadata.GetDisplayName()), Membership.MinRequiredPasswordLength);
yield return rule;
}
}
}
下面是JS代码,我的问题:
jQuery.validator.addMethod("minrequiredpasswordlength", function (value, element, params) {
if (value.length == 0) {
return true; //empty
}
return true; //dummy return
}, "");
jQuery.validator.unobtrusive.adapters.add("minrequiredpasswordlength", {}, function (options) {
//XXX Here I should get the minlength parameter, but 'options' is a empty object
options.messages["minrequiredpasswordlength"] = options.message;
});
非常感谢你的帮助 查看此链接是否有帮助: 另外,请尝试将javascript代码更改为:
$.validator.addMethod('minrequiredpasswordlength', function (value, element, params) {
var minLength = params.minLength;
// minLength should contain your value.
return true; // dummy return
});
jQuery.validator.unobtrusive.adapters.add('minrequiredpasswordlength', [ 'minlength' ], function (options) {
options.rules['minrequiredpasswordlength'] = {
minLength: options.params['minlength']
};
//XXX Here I should get the minlength parameter, but 'options' is a empty object
options.messages["minrequiredpasswordlength"] = options.message;
});
这里的秘密是用['minlength']代替{}当我第一次学会这个时,我花了很多时间。我希望这能有所帮助。终于!在这段时间之后,我最终使用了另一种方法,即从HTML属性中获取minLength,而不是从“params”中获取minLength。但是这个解决方案要好得多,谢谢!是的,将参数传递给客户端的过程没有很好的文档记录。我在网上的其他地方找到了它,同时搜索其他任何东西。我希望这可以帮助其他人(包括我自己)在未来。当做
$.validator.addMethod('minrequiredpasswordlength', function (value, element, params) {
var minLength = params.minLength;
// minLength should contain your value.
return true; // dummy return
});
jQuery.validator.unobtrusive.adapters.add('minrequiredpasswordlength', [ 'minlength' ], function (options) {
options.rules['minrequiredpasswordlength'] = {
minLength: options.params['minlength']
};
//XXX Here I should get the minlength parameter, but 'options' is a empty object
options.messages["minrequiredpasswordlength"] = options.message;
});