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

标签 asp.net-mvc asp.net-mvc-3 jquery-ui data-annotations asp.net-mvc-validation

我有一个 RangeValidator在我的模型中的一个属性上只允许 0 到 100 之间的整数。我有一个部分 View ,它显示一个表单以通过 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; }
  ...
}

局部 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>

产生的标记:
<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>

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

最佳答案

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

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

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

关于asp.net-mvc - MVC 数据注释范围验证无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15370768/

相关文章:

c# - 在 MVC 中使用 CasSTLe Windsor 和 FluentValidation

javascript - 调用javascript函数来获取元素的偏移量

javascript - 排序时和排序后边距和对齐方式发生变化

asp.net-mvc - 使用 ViewModel 值初始化模型

asp.net - 使用 Signalr 拥有一个类似 facebook 的通知系统

asp.net - 如何使用 AutoMapper 将多个对象映射到一个对象 - asp.net mvc 3

javascript - 带边框的 Div 在 dragend 上调整大小

c# - ConnectionStrings 或 AppSettings 的存储库

c# - MVC Web Api 作为中间层服务

c# - 返回一个字符串列表作为 JSONResult