How to specify a min but no max decimal using the range data annotation attribute?

后端 未结 10 1720
旧时难觅i
旧时难觅i 2021-01-31 01:02

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

10条回答
  •  温柔的废话
    2021-01-31 01:34

    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.")]
    

提交回复
热议问题