我有一个
RangeValidator
在我的模型中的属性上,只允许0到100之间的整数。我有一个局部视图,它显示了一个通过jQueryUI对话框更新属性的窗体。我已经查看了源代码,可以确认数据注释属性是正确生成的。然而,验证工作不正常。它确实执行了某种验证,但它没有使用我设置的范围。值1、10和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; }
...
}
局部视图:
@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
,如果我改变我的
TextBoxFor
到
EditorFor
它会变成
number
但我仍然看到同样的行为。