如何使用范围数据注释属性指定最小但没有最大小数?

发布于 2024-09-11 19:22:53 字数 182 浏览 5 评论 0原文

我想指定价格的小数字段必须 >= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

你げ笑在眉眼 2024-09-18 19:23:04

我会放置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.

眼藏柔 2024-09-18 19:23:03

[Range(0.01,100000000,ErrorMessage = "价格必须大于零!")]

[Range(0.01,100000000,ErrorMessage = "Price must be greter than zero !")]

请恋爱 2024-09-18 19:23:03

迟到的答案,但我有这个:

[Range(1, int.MaxValue)]
public int MyInteger { get; set; }

用你想要的任何数字类型替换 int 。您还可以将 1 替换为 int.MinValue。

Late answer but I have this:

[Range(1, int.MaxValue)]
public int MyInteger { get; set; }

replace int by whatever numeric type you want. You can also replace 1 with int.MinValue.

邮友 2024-09-18 19:23:02

我打算尝试这样的事情:

[Range(typeof(decimal), ((double)0).ToString(), ((double)decimal.MaxValue).ToString(), ErrorMessage = "Amount must be greater than or equal to zero.")]

不过,这样做的问题是编译器需要一个常量表达式,它不允许 ((double)0).ToString()。编译器采用

[Range(0d, (double)decimal.MaxValue, ErrorMessage = "Amount must be greater than zero.")]

I was going to try something like this:

[Range(typeof(decimal), ((double)0).ToString(), ((double)decimal.MaxValue).ToString(), ErrorMessage = "Amount must be greater than or equal to zero.")]

The problem with doing this, though, is that the compiler wants a constant expression, which disallows ((double)0).ToString(). The compiler will take

[Range(0d, (double)decimal.MaxValue, ErrorMessage = "Amount must be greater than zero.")]
若相惜即相离 2024-09-18 19:23:02

使用 Range

[Range(typeof(Decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]

[Range(typeof(Decimal),"0.0", "1000000000000000000"]

希望它会有所帮助

using Range with

[Range(typeof(Decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]

[Range(typeof(Decimal),"0.0", "1000000000000000000"]

Hope it will help

意中人 2024-09-18 19:23:01

您可以使用自定义验证:

    [CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
    public int IntValue { get; set; }

    [CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
    public decimal DecValue { get; set; }

验证方法类型:

public class ValidationMethods
{
    public static ValidationResult ValidateGreaterOrEqualToZero(decimal value, ValidationContext context)
    {
        bool isValid = true;

        if (value < decimal.Zero)
        {
            isValid = false;
        }

        if (isValid)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult(
                string.Format("The field {0} must be greater than or equal to 0.", context.MemberName),
                new List<string>() { context.MemberName });
        }
    }
}

You can use custom validation:

    [CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
    public int IntValue { get; set; }

    [CustomValidation(typeof(ValidationMethods), "ValidateGreaterOrEqualToZero")]
    public decimal DecValue { get; set; }

Validation methods type:

public class ValidationMethods
{
    public static ValidationResult ValidateGreaterOrEqualToZero(decimal value, ValidationContext context)
    {
        bool isValid = true;

        if (value < decimal.Zero)
        {
            isValid = false;
        }

        if (isValid)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult(
                string.Format("The field {0} must be greater than or equal to 0.", context.MemberName),
                new List<string>() { context.MemberName });
        }
    }
}
音盲 2024-09-18 19:23:00

如果您正在研究价格,我相信您可以放心地假设没有任何东西的成本会超过 1 万亿美元。

我会使用:

[Range(0.0, 1000000000000)]

或者,如果您确实需要它,只需粘贴 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:

[Range(0.0, 1000000000000)]

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.

十六岁半 2024-09-18 19:22:59

看来除了手动输入最大值之外别无选择。我希望存在某种类型的过载,您无需指定它。

[Range(typeof(decimal), "0", "79228162514264337593543950335")]
public decimal Price { get; set; }

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.

[Range(typeof(decimal), "0", "79228162514264337593543950335")]
public decimal Price { get; set; }
眉黛浅 2024-09-18 19:22:59

您可以使用:

[Min(0)]

这将强制要求最小值 0(零),并且没有最大值。

您需要 DataAnnotationsExtensions 才能使用它。

You can use:

[Min(0)]

This will impose a required minimum value of 0 (zero), and no maximum value.

You need DataAnnotationsExtensions to use this.

薄荷→糖丶微凉 2024-09-18 19:22:58

如果您担心字符串看起来不错,您可以这样做:

    [Range(0, Double.PositiveInfinity)]

这将有一个默认错误消息:

字段 SuchAndSuch 必须介于 0 和 Infinity 之间。

If you are concerned about the string looking nice you could do this:

    [Range(0, Double.PositiveInfinity)]

This will have a default error message of:

The field SuchAndSuch must be between 0 and Infinity.

烂人 2024-09-18 19:22:57

像这样的事情怎么样:

[Range(0.0, Double.MaxValue, ErrorMessage = "The field {0} must be greater than {1}.")]

这应该可以满足您的要求,并且您可以避免使用字符串。

How about something like this:

[Range(0.0, Double.MaxValue, ErrorMessage = "The field {0} must be greater than {1}.")]

That should do what you are looking for and you can avoid using strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文