I found an ugly workaround.
It seems that if category_id is a null int? value int? , and my value is not a valid number, an empty value is passed, and the model does not see the invalid value "abc".
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")] public int? category_id { get; set; }
If I change category_id to a non-nullable int , it will not check even when the value is not passed.
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")] public int? category_id { get; set; }
Ugly workaround
If I changed category_id to string and then only converted it to int , when I need it, I can check it correctly using only [Range]
[Range(0, 999999, ErrorMessage = "category_id must be a valid number")] public string category_id { get; set; }
This is ugly, but it works.
(Note: a custom attribute is not needed, so I deleted it and just used [Range])
source share