int的范围被视为字符串

时间:2013-03-26 20:29:18

标签: c# entity-framework asp.net-mvc-4 jquery-validate

我有一个用C#,MVC4 EF(数据优先)制作的项目。

在我的“创建”视图中,我有许多数字字段,我想使用范围为其指定允许的值。由于一些奇怪的原因,我的数字被视为字符串,这会影响验证。

让我们以“LengthInch”字段为例: 在数据库中,它是一个int。

在我的视图中,我有代码:

@Html.EditorFor(model => model.LengthInch)
@Html.ValidationMessageFor(model => model.LengthInch)

最后在我的元数据文件中(对于模型)我有代码:

[Range(0, 11, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
public Nullable<int> LengthInch { get; set; }

如果我在字段中输入0,1,10或11,一切都很好,但如果我输入2,我会收到验证错误,说“LongInnch的值必须介于0和11之间”。同样适用于3到9.所以看起来这些数字由于某种原因被视为字符串。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

马尔科姆的“新”项目工作指向了正确的方向,谢谢!似乎jquery.validate的人做了一个针对issue的错误修复推送。

        // convert the value to a number for number inputs, and for text for backwards compability
        // allows type="date" and others to be compared as strings
        if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
            value = Number(value);
        }

        if ( value ) {
            rules[method] = value;
        } else if ( type === method && type !== 'range' ) {
            // exception: the jquery validate 'range' method
            // does not test for the html5 'range' type
            rules[method] = true;
        }

一旦我对jqueryvalidation-1.11.1进行了nuget更新,就修复了这个问题,欢迎来到JS包地狱!