使用dataannotations进行ASP.NET客户端验证 - javascript minimumlength zero

时间:2010-04-27 11:53:37

标签: javascript asp.net-mvc validation

我正在对我正在进行的项目进行客户端验证。一切正常,除了StringLength属性的minimumlength属性(它在提交和服务器端验证完成时有效):

[StringLength(50, MinimumLength = 6)]

Html.EnableClientValication();生成的javascript如下:

// snip 
{"FieldName":"User.Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"User_Password_validationMessage","ValidationRules":[{"ErrorMessage":"The field Password must be a string with a minimum length of 6 and a maximum length of 50.","ValidationParameters":{"minimumLength":0,"maximumLength":50},"ValidationType":"stringLength"}]}],"FormId":"form0","ReplaceValidationSummary":false})

重要的是:

{"minimumLength":0,"maximumLength":50}

它使用错误的minimumproperty生成javascript。你们有提示吗?这可能是一个错误吗?

1 个答案:

答案 0 :(得分:2)

这是来自反射器的代码

    public class StringLengthAttributeAdapter : DataAnnotationsModelValidator<StringLengthAttribute>
    {
        // Methods
        public StringLengthAttributeAdapter(ModelMetadata metadata, ControllerContext context, StringLengthAttribute attribute) : base(metadata, context, attribute)
        {
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
           return new ModelClientValidationStringLengthRule[] 
{ new ModelClientValidationStringLengthRule(base.ErrorMessage, 
-> 0 <-, base.Attribute.MaximumLength) };
        }
    }
是的,这是一个非常不对的。