I thought to just check the definition of the data annotation attribute before giving you some alternatives. I was surprised to see that AttributeUsage defined in such a way that you can also apply to the parameters of the method, and, unfortunately, it did not work, since I tried myself. Although I'm not sure why they allowed this parameter to the method parameters (I see it as valid, but it doesnβt work)
As for alternatives, you can try this,
public ActionResult Test(int i) { var rangeAttr = new RangeAttribute(0, 10); if(!rangeAttr.IsValid(i)) ModelState.AddModelError(i.ToString(), rangeAttr.FormatErrorMessage("i")); }
source share