如何使用范围数据注释属性指定最小但没有最大小数?
我想指定价格的小数字段必须 >= 0,但我真的不想强加最大值。
这是我到目前为止所拥有的......我不确定执行此操作的正确方法是什么。
[Range(typeof(decimal), "0", "??"] public decimal Price { get; set; }
I would like to specify that a decimal field for a price must be >= 0 but I don't really want to impose a max value.
Here's what I have so far...I'm not sure what the correct way to do this is.
[Range(typeof(decimal), "0", "??"] public decimal Price { get; set; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我会放置decimal.MaxValue.ToString(),因为这是十进制类型的有效上限,它相当于没有上限。
I would put
decimal.MaxValue.ToString()
since this is the effective ceiling for the decmial type it is equivalent to not having an upper bound.[Range(0.01,100000000,ErrorMessage = "价格必须大于零!")]
[Range(0.01,100000000,ErrorMessage = "Price must be greter than zero !")]
迟到的答案,但我有这个:
用你想要的任何数字类型替换 int 。您还可以将 1 替换为 int.MinValue。
Late answer but I have this:
replace int by whatever numeric type you want. You can also replace 1 with int.MinValue.
我打算尝试这样的事情:
不过,这样做的问题是编译器需要一个常量表达式,它不允许
((double)0).ToString()
。编译器将采用I was going to try something like this:
The problem with doing this, though, is that the compiler wants a constant expression, which disallows
((double)0).ToString()
. The compiler will take使用 Range
希望它会有所帮助
using Range with
Hope it will help
您可以使用自定义验证:
验证方法类型:
You can use custom validation:
Validation methods type:
如果您正在研究价格,我相信您可以放心地假设没有任何东西的成本会超过 1 万亿美元。
我会使用:
或者,如果您确实需要它,只需粘贴
Decimal.MaxValue
的值(不带逗号):79,228,162,514,264,337,593,543,950,335
如果满足以下条件,其中任何一个都可以正常工作你不是来自津巴布韦。
If you're working with prices, I'm sure you can safely assume nothing will cost more than 1 trillion dollars.
I'd use:
Or if you really need it, just paste in the value of
Decimal.MaxValue
(without the commas):79,228,162,514,264,337,593,543,950,335
Either one of these will work well if you're not from Zimbabwe.
看来除了手动输入最大值之外别无选择。我希望存在某种类型的过载,您无需指定它。
It seems there's no choice but to put in the max value manually. I was hoping there was some type of overload where you didn't need to specify one.
您可以使用:
这将强制要求最小值 0(零),并且没有最大值。
您需要 DataAnnotationsExtensions 才能使用它。
You can use:
This will impose a required minimum value of 0 (zero), and no maximum value.
You need DataAnnotationsExtensions to use this.
如果您担心字符串看起来不错,您可以这样做:
这将有一个默认错误消息:
If you are concerned about the string looking nice you could do this:
This will have a default error message of:
像这样的事情怎么样:
这应该可以满足您的要求,并且您可以避免使用字符串。
How about something like this:
That should do what you are looking for and you can avoid using strings.