如何使用数据注释在 mvc 中验证文本框输入是正整数还是负整数

How can I validate a textbox input to be a positive or negative integer in mvc using dataannotations

用户可以输入正浮点数和负浮点数。目前我正在使用

[Range(0, double.MaxValue, ErrorMessage = "Only integers are allowed")]

但是当我输入以 - 符号开头的负数时,它给我验证错误。

把你RangeAttribute改成

[Range(double.MaxMinValue, double.MaxValue, ErrorMessage = "Only integers are allowed")]

旁注:我认为错误消息应该是 "Only numbers are.."?您允许浮点数,而不仅仅是整数,但是如果 属性 您将其应用于 typeof double,则实际上不需要 RangeAttribute