【问题标题】:Integer Input validation asp.net mvc4整数输入验证asp.net mvc4
【发布时间】:2012-09-04 11:45:35
【问题描述】:

我想验证来自文本框的输入,使其为整数且范围为 0-255。我有一个小的 asp.net mvc4 项目

【问题讨论】:

标签: asp.net asp.net-mvc-4


【解决方案1】:

使用数据注解:http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-6

[Required(ErrorMessage = "Value must be set")]
[Range(0, 250, ErrorMessage = "Value must be between 0 and 250")]
public int Value{ get; set; }

【讨论】:

  • 这不起作用。键入“7”,您仍然会看到错误消息。 ASP.NET MVC 或 jquery-validation 中存在一个错误 - 它将您的值转换为字符串并比较字符串而不是整数
  • 看起来该错误在 Jquery 中,我将其复制到:/*! jQuery 验证插件 - v1.11.0 - 2/4/2013
【解决方案2】:

对于任何整数,请使用以下方法:

[Required(ErrorMessage = "Value must be set")]
[Range(0, int.MaxValue, ErrorMessage = "Value must be between 0 and 250")]
public int Value{ get; set; `enter code here`}

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 2013-05-31
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2010-09-14
    相关资源
    最近更新 更多