Assuming that you have used DataAnnotations in your model to validate, example [Required]
so that the field is not null or [DataType(DataType.Date)]
to validate that the field is of type Date, you can also create a Custom Validation, in this case for that the last number is less than 100. Example
in your Models folder creates a new name class LessQue100Attribute with the following code.
public class MenorQue100Attribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var numero = int.Parse(value.ToString());
if (numero > 100)
{
return new ValidationResult(ErrorMessage = "El número es mayor que 100");
}
return ValidationResult.Success;
}
}
and in your model you can already add that dataannotation as when you add [Required]
example:
using System.ComponentModel.DataAnnotations;
public class Auto
{
[Key, ForeignKey("Conductor")]
public int ConductorId { get; set; }
public string Modelo { get; set; }
[MenorQue100]
public int Millas { get; set; }
public virtual Conductor Conductor { get; set; }
}
This way you have created a CustomValidation which does not allow you to add an Auto with more than 100 miles. I hope it helps you
I have edited my answer because as Gbianchi reminded me, there is already a DataAnnotation for this type of problem [Range]
example;
using System.ComponentModel.DataAnnotations;
public class Auto
{
[Key, ForeignKey("Conductor")]
public int ConductorId { get; set; }
public string Modelo { get; set; }
[Range(0,100, ErrorMessage = "Mayor que 100")]
public int Millas { get; set; }
public virtual Conductor Conductor { get; set; }
}
In any case it is good to know the first way if there is a case in which it is necessary to use a validation type which is not found in the DataAnnotations. Greetings