简体   繁体   English

MVC非侵入式验证未正确触发

[英]MVC Unobtrusive Validation Incorrectly Firing

We use data annotations in combination with the jQuery.validate.unobtrusive plugin for one of our applications and came across a unique, what appears to be a bug, today. 对于我们的一个应用程序,我们将数据注释与jQuery.validate.unobtrusive插件结合使用,并且遇到了一个独特的,看起来像是个错误的今天。

View Model Property 查看模型属性

[Required(ErrorMessage = "You must specifiy a point value for the contest entry")]
[Min(5, ErrorMessage = "The point value must be at least 5 points for this contest")]
[DisplayName("Points required for entry:")]
public int RequiredPoints { get; set; }

View 视图

<li class="reqpoints">@Html.LabelFor(x => x.RequiredPoints) @Html.TextBoxFor(x => x.RequiredPoints)</li>

Resulting HTML 产生的HTML

<li class="reqpoints">

    <label for="RequiredPoints"></label>
    <input id="RequiredPoints" class="input-validation-error" type="text" value="10" name="RequiredPoints" data-val-required="You must specifiy a point value for the contest entry" data-val-range-min="5" data-val-range="The point value must be at least 5 points for this contest" data-val-number="The field Points required for entry: must be a number." data-val="true"></input>

</li>

This works great if the number entered into the textbox is 5-9 or if the first digit is 5 or greater. 如果在文本框中输入的数字是5-9或第一位数字是5或更大,则此方法非常有用。 Any number from 10 through 49, 100-499, 1000-4999, etc gets rejected with the error message that it must be at least 5 points, almost as if it is truncating the last digits. 从10到49、100-499、1000-4999等中的任何数字都将被拒绝,并显示错误消息,提示它必须至少为5分,几乎就像它在截断最后一位数字一样。 This is not going to the server and coming back, it is the jQuery validation portion that is firing off. 这不会发送到服务器再返回,而是触发了jQuery验证部分。 Has anyone seen an issue using Min() with a single integer value like this? 有没有人看到使用Min()的单个整数值的问题?

I should note we use this somewhere else where the minimum value is 100 and have no issues (that I am aware of) of any number greater than 100 being rejected. 我应该注意,我们在最小值为100且没有任何大于100的数字(我知道)的地方被拒绝使用。

Relevant versions: 相关版本:

  • jQuery - 1.8.4 jQuery-1.8.4
  • jQuery.Validation - 1.12.0 jQuery.Validation-1.12.0
  • jQuery.validation.unobtrusive - 3.1.2 jQuery.validation.unobtrusive-3.1.2

Update 更新

If I change the Min attribute to have a minimum of 10, everything works as expected. 如果我将Min属性更改为Min 10,则一切都会按预期工作。 Additionally, if I use the Range(5,99999) data attribute, then all values seem to work as expected as well. 此外,如果我使用Range(5,99999)数据属性,那么所有值似乎也都可以正常工作。 It appears that the combination of jQuery 1.8.4 and the latest versions of the validation plugins might have an issue with single digit minimum validation. 看来jQuery 1.8.4和最新版本的验证插件的结合可能会出现一位数最小验证问题。

This is a project we have been supporting for a few years now and I had to refresh some knowledge on the project. 这是我们已经支持了几年的项目,我不得不刷新一些关于该项目的知识。

The Min() attribute is actually part of the DataAnnotationExtensions package that adds some additional data annotations and plugs in with the jQuery validation framework. Min()属性实际上是DataAnnotationExtensions包的一部分,该包添加了一些其他数据注释,并使用jQuery验证框架进行了插入。 Installing this package along with jQuery validation framework (all up to date) I can replicate this issue on a fresh, brand new project. 将这个软件包与jQuery验证框架一起安装(都是最新的),我可以在一个全新的项目中复制此问题。

I will report this issue to the package authors but will leave this here in case anyone else using this package runs across this issue. 我将向软件包作者报告此问题,但如果使用此软件包的其他任何人遇到此问题,则将其保留在此处。 The work around is to use the Range() data annotation. 解决方法是使用Range()数据批注。

Update 更新

Issue reported here - however, it does not appear this project has been officially updated in some time. 这里报告了问题 -但是,似乎这个项目在一段时间内没有正式更新。 I may examine how we use this project and replace its components with official .NET bits. 我可能会检查我们如何使用该项目,并用.NET官方位替换其组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM