【问题标题】:mvc4 validation numbers throws exceptionmvc4 验证号抛出异常
【发布时间】:2013-11-08 10:22:39
【问题描述】:

我的模型中有一个字段,我希望它只是数字

[Required(ErrorMessage = "Cheque ID is required")]
[RegularExpression("([1-9][0-9])*", ErrorMessage = "Cheque ID must be numbers only.")]
[Range(typeof(Decimal), "1", "9999", ErrorMessage = "Cheque ID must be a number between {1} and {999}.")]

我试过了:

在我看来,我使用了这个

<div class="editor-field">
    @Html.TextBoxFor(x => x.chequeID, new {placeholder = "Enter The Location", @Value = "" })
    @Html.ValidationMessageFor(model => model.chequeID)
</div>

但是我去这个例外

索引(从零开始)必须大于或等于零且小于参数列表的大小。

我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc validation asp.net-mvc-4 razor


    【解决方案1】:

    你的问题在这里:

    [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Cheque ID must be a number between {1} and {999}.")]
    

    此索引{999} 超出范围。与string.Format 一样,如果您有3 个参数,则您的字符串应按照指定的顺序将它们包括为{0}{1}{2}

    在您的情况下,将{999} 替换为{2} 将显示“9999”作为最大值({0} 是属性名称,这就是为什么最小值是{1})。

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2015-11-14
      相关资源
      最近更新 更多