MVC数据注解范围验证工作不正常 [英] MVC data annotations range validation not working properly

查看:128
本文介绍了MVC数据注解范围验证工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RangeValidator控件在我的模型的属性,只允许那些介于0和100我有一个显示一个表单来更新属性的局部视图整数通过jQuery UI的对话框。我已经看过源,可以确认正在正确生成的数据标注属性。然而,validatation不能正常工作。它确实执行某种验证,但是它没有使用我设置的范围内。值1,10和100不产生误差。其他任何一个或两个数字的价值产生错误。不过,如果我垫零,所有值不到一百的罚款。

型号:

 公共类为MyModel
{
  ...
  [必需(的ErrorMessage ={0}要求)]
  [范围(typeof运算(INT),0,100,
         的ErrorMessage ={0}只能之间{1}和{2})]
  公众诠释百分比{搞定;组; }
  ...
}

局部视图:

  @model MyApp.Models.Partials.MyModel< D​​IV ID =myDialog级=editModal称号=更新百分比>
  @using(Ajax.BeginForm(UpdatePercentage,myController的,
                         新AjaxOptions(){列举HTTPMethod =邮报,
                                             的onSuccess =onUpdateSuccess},
                         新{NAME =myForm会}))
  {
    @ Html.ValidationSummary(空,新的{风格=宽度:汽车;最大宽度:22em;
                                                 浮动:左;明确:既;})
    < D​​IV的风格=宽度:汽车;浮动:左;明确:既;>
      < D​​IV的风格=宽度:10em;浮动:左;文本对齐:权利;明确:左;>
        @ Html.Label(百分比)
      < / DIV>
      < D​​IV的风格=宽度:12em;保证金左:1EM;浮动:左;明确:右;>
        @ Html.TextBoxFor(M = GT; m.Percentage)
      < / DIV>
    < / DIV>
    < D​​IV的风格=宽度:23em;明确:既;文本对齐:中心;>
      <小时/>
      <输入类型=按钮值=取消级=cancelModalButton
             数据模式=myDialog/>
      <输入类型=提交值=提交级=submitModalButton
             数据模态=myDialog数据型=myForm会/>
    < / DIV>
  }
< / DIV>

标记产生的:

 < D​​IV ID =myDialog级=editModal称号=更新百分比>
  <形式的行动=?/网络/ myController的/ UpdatePercentage长度= 3
        数据阿贾克斯=真正的数据Ajax的方法=邮报的数据,Ajax的成功=onUpdateSuccess
        ID =窗体2的方法=后NAME =myForm会>
    < D​​IV CLASS =验证,汇总有效的数据valmsg-总结=真
         风格=宽度:汽车;最大宽度:22em;浮动:左;明确:既;>
      < UL>
        <李风格=显示:无>< /李>
     < / UL>
    < / DIV>
    < D​​IV的风格=宽度:汽车;浮动:左;明确:既;保证金底:.75em;>
      < D​​IV的风格=宽度:10em;浮动:左;文本对齐:权利;明确:左;>
        <标签=比例:>百分比:其中; /标签>
      < / DIV>
      < D​​IV的风格=宽度:12em;保证金左:1EM;浮动:左;明确:右;>
        <输入数据-VAL =真正的数据-VAL-数=这个领域百分比必须是一个数字。
               数据-VAL-范围=百分比只能是0和100之间
               数据-VAL-范围-最大值=100数据-VAL-范围分钟=0
               数据-VAL-所需=百分比是必需的ID =百分比
               NAME =百分比类型=文本值=10/>
      < / DIV>
    < / DIV>
    < D​​IV的风格=宽度:23em;明确:既;文本对齐:中心;>
      <小时/>
      <输入类型=按钮值=取消级=cancelModalButton数据模式=myDialog/>
      <输入类型=提交值=提交级=submitModalButton数据模式=myDialog数据型=myForm会/>
    < / DIV>
  < /表及GT;
< / DIV>

我看到类型设置为文本,如果我改变我的 TextBoxFor EditorFor 它chnages到数量,但我仍然看到相同的行为。


解决方案

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

jQuery验证(至少1.11.1)结果
微软不显眼的jQuery验证(至少2.0.30116.0)结果
微软不显眼的jQuery的阿贾克斯(至少2.0.30116.0)

问题是由jQuery的1.9.1重大更改出台

I have a RangeValidator on 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.

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 TextBoxFor to EditorFor it chnages to number but I still see the same behavior.

解决方案

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)

The problem was introduced by breaking changes in jQuery 1.9.1

这篇关于MVC数据注解范围验证工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆