asp.net-mvc Data annotations Range Attribute

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The Range attribute can decorate any properties or public fields and specifies a range that a numerical field must fall between to be considered valid.

[Range(minimumValue, maximumValue)]
public int Property { get; set; }

Additionally, it accepts an optional ErrorMessage property that can be used to set the message received by the user when invalid data is entered :

[Range(minimumValue, maximumValue, ErrorMessage = "{your-error-message}")]
public int Property { get; set; }

Example

[Range(1,100, ErrorMessage = "Ranking must be between 1 and 100.")]
public int Ranking { get; set; }


Got any asp.net-mvc Question?