asp.net-mvc-4

Validation for numeric but not equal to 0 in MVC4


I want to use DataAnnotations in MVC4 for valid value only for numeric but greater than 0.


Solution

  • You could use the [Range] attribute:

    [Range(1, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
    public int Value { get; set; }