无法访问.NET MVC客户端验证上的ValidationParameters

时间:2013-01-25 13:06:00

标签: asp.net-mvc validation validationattribute validationrule

我正在为我的.NET MVC 4应用程序编写自定义验证。这是第一个使用参数的验证,我发现这有点麻烦。

这是我的C#代码:

ValidationAttribute:

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;
        }
    }
}

ModelClientValidationRule:

namespace Validations
{
    public class ModelClientMinRequiredPasswordLengthValidationRule : ModelClientValidationRule
    {
        public ModelClientMinRequiredPasswordLengthValidationRule(string errorMessage, int minRequiredPasswordLength)
        {
            ErrorMessage = errorMessage;
            ValidationType = "minrequiredpasswordlength";
            ValidationParameters.Add("minlength", minRequiredPasswordLength);
        }
    }
}

这是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;
});

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

查看此链接是否有帮助:

ASP.NET MVC 3 client-side validation with parameters

另外,请尝试将您的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']而不是{}当我第一次学习这个时,我花了很多时间。我希望这会有所帮助。