问题
This is the property declaration in question:
[RangeValidator(1,RangeBoundaryType.Inclusive,255,RangeBoundaryType.Inclusive,MessageTemplate = "StartFlexibility is out of range")]
public byte StartFlexibility { get; set; }
When the validate method is called, a FormatException is thrown telling me that the value type needs to be Int32.
How to fix, please?
回答1:
well... the quick obvious fix will be change the type to short or int,
but another observation i want to do, is with the range. You are telling the RangeValidator to take a inclusive range between 1 and 256, but you just can assign a byte value till 255, maybe that's the compiler reason to cry out.
The RangeValidator is also infering the type of the Range from the parameters, so, try casting
[RangeValidator((byte) 1, ...
回答2:
As Jhonny says, cast to byte... but more like this
[RangeValidator(typeof(Byte), "1", RangeBoundaryType.Inclusive, "255", RangeBoundaryType.Inclusive, MessageTemplate = "Some message")]
The other option would be to call the range validator in a SelfValidation message and cast there.
回答3:
I've never used the RangeValidator class/attribute, but is it an issue that you have 256 as an upper bound when a byte can only get to 255?
来源:https://stackoverflow.com/questions/727017/using-rangevalidator-with-byte