MVC数据注释范围验证无法正常工作

sto*_*her 19 asp.net-mvc jquery-ui asp.net-mvc-validation data-annotations asp.net-mvc-3

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

模型:

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; }
  ...
}
Run Code Online (Sandbox Code Playgroud)

局部视图:

@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>
Run Code Online (Sandbox Code Playgroud)

标记产生:

<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>
Run Code Online (Sandbox Code Playgroud)

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

Col*_*lin 7

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

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

通过破坏jQuery 1.9.1中的更改​​引入了该问题