【问题标题】:Can't acces the ValidationParameters on .NET MVC client-side validation无法访问 .NET MVC 客户端验证上的 ValidationParameters
【发布时间】:2013-01-09 10:50:48
【问题描述】:

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

这是我的 C# 代码:

验证属性:

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

非常感谢您的帮助!

【问题讨论】:

    标签: asp.net-mvc validation validationattribute validationrule


    【解决方案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' ] 而不是 {} 我第一次学习这个时花了很多时间。我希望这会有所帮助。

    【讨论】:

    • 终于!在所有这些时间之后,我最终使用了一种替代方法,涉及从 HTML 属性而不是从“参数”中获取 minLength。但是这个解决方案要好得多,谢谢!
    • 是的,参数传递到客户端的记录不是很好。我在网络上的其他地方找到它,同时搜索其他任何东西。我希望这可以在未来帮助其他人(包括我自己)。问候!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多