asp.net-mvc MVC 数据注释范围验证无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15370768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:39:00  来源:igfitidea点击:

MVC data annotations range validation not working properly

asp.net-mvcasp.net-mvc-3jquery-uidata-annotationsasp.net-mvc-validation

提问by stopher

I have a RangeValidatoron a property in my model to only allow Integers that are between 0 and 100. I have a partial view that displays a form to update the property via a jQuery UI dialog. I have looked at the source and can confirm that the data annotation attributes are being generated properly. However, the validatation isn't working properly. It does perform some kind of validation, but it isn't using the range I'm setting. The values 1, 10, and 100 do not produce the error. Any other single or two digit value produces the error. However, if I pad with zeros, all values less than one hundred are fine.

我的模型中有一个RangeValidator属性,只允许 0 到 100 之间的整数。我有一个局部视图,显示一个表单以通过 jQuery UI 对话框更新属性。我查看了源代码,可以确认数据注释属性正在正确生成。但是,验证工作不正常。它确实执行了某种验证,但它没有使用我设置的范围。值 1、10 和 100 不会产生错误。任何其他一位或两位数字值都会产生错误。但是,如果我用零填充,则所有小于 100 的值都可以。

Model:

模型:

public class MyModel
{
  ...
  [Required(ErrorMessage = "{0} is required")]
  [Range(typeof(int), "0", "100", 
         ErrorMessage = "{0} can only be between {1} and {2}")]
  public int Percentage { get; set; }
  ...
}

Partial View:

部分视图:

@model MyApp.Models.Partials.MyModel

<div id="myDialog" class="editModal" title="Update Percentage">
  @using (Ajax.BeginForm("UpdatePercentage", "MyController", 
                         new AjaxOptions() { HttpMethod = "Post", 
                                             OnSuccess = "onUpdateSuccess" }, 
                         new { name = "myForm" }))
  {
    @Html.ValidationSummary(null, new { style = "width:auto;max-width:22em;               
                                                 float:left;clear:both;" })    
    <div style="width:auto;float:left;clear:both;">
      <div style="width:10em;float: left;text-align:right;clear:left;">
        @Html.Label("Percentage:")
      </div>
      <div style="width:12em;margin-left:1em;float:left;clear:right;">
        @Html.TextBoxFor(m => m.Percentage)
      </div>
    </div>
    <div style="width:23em;clear:both;text-align:center;">
      <hr />
      <input type="button" value="Cancel" class="cancelModalButton" 
             data-modal="myDialog" />
      <input type="submit" value="Submit" class="submitModalButton" 
             data-modal="myDialog" data-form="myForm" />
    </div>
  }
</div>

Markup produced:

标记产生:

<div id="myDialog" class="editModal" title="Update Percentage">
  <form action="/Web/MyController/UpdatePercentage?Length=3" 
        data-ajax="true" data-ajax-method="Post" data-ajax-success="onUpdateSuccess" 
        id="form2" method="post" name="myForm">
    <div class="validation-summary-valid" data-valmsg-summary="true" 
         style="width:auto;max-width:22em;float:left;clear:both;">
      <ul>
        <li style="display:none"></li>
     </ul>
    </div>
    <div style="width:auto;float:left;clear:both;margin-bottom:.75em;">
      <div style="width:10em;float: left;text-align:right;clear:left;">
        <label for="Percentage:">Percentage:</label>
      </div>
      <div style="width:12em;margin-left:1em;float:left;clear:right;">
        <input data-val="true" data-val-number="The field Percentage must be a number." 
               data-val-range="Percentage can only be between 0 and 100" 
               data-val-range-max="100" data-val-range-min="0" 
               data-val-required="Percentage is required" id="Percentage" 
               name="Percentage" type="text" value="10" />
      </div>
    </div>
    <div style="width:23em;clear:both;text-align:center;">
      <hr />
      <input type="button" value="Cancel" class="cancelModalButton" data-modal="myDialog" />
      <input type="submit" value="Submit" class="submitModalButton" data-modal="myDialog" data-form="myForm" />
    </div>
  </form>
</div>

I see that the type is set to text, and if I change my TextBoxForto EditorForit chnages to numberbut I still see the same behavior.

我看到类型设置为text,如果我TextBoxForEditorFor它更改为 ,number但我仍然看到相同的行为。

采纳答案by Colin

I had this exact problem and found the solution here. You need to upgrade the following:

我遇到了这个确切的问题,并在这里找到了解决方案。您需要升级以下内容:

jQuery Validation (at least 1.11.1)
Microsoft jQuery Unobtrusive Validation (at least 2.0.30116.0)
Microsoft jQuery Unobtrusive Ajax (at least 2.0.30116.0)

jQuery 验证(至少 1.11.1)
Microsoft jQuery Unobtrusive 验证(至少 2.0.30116.0)
Microsoft jQuery Unobtrusive Ajax(至少 2.0.30116.0)

The problem was introduced by breaking changes in jQuery 1.9.1

该问题是由 jQuery 1.9.1 中的重大更改引入的

回答by Display Name

Try [Range(0, 100)]. Remove ErrorMessagecompletely to see that you not breaking anything with wrong formatting and curly brackets.

试试[Range(0, 100)]ErrorMessage完全删除以查看您没有用错误的格式和大括号破坏任何内容。

If pure range doesn't work properly let force (MS) be with you! :)

如果纯范围不能正常工作,让力 (MS) 与您同在!:)