Probably I am missing something, but having the model below
public class MyModel
{
public double WhateverButNotZero { get; set; }
}
is there any MVC built-in DataAnnotation to validate the number as "everything but zero"?
There is no built-in validation for that specifically, but you can create a custom attribute for it if you don't want to use regex as mentioned in other answers.
ValidationAttribute
classIsValid(object value)
methodint
and check if it's equal zeropublic class NotZeroAttribute : ValidationAttribute
{
public override bool IsValid(object value) => (int)value != 0;
}
Then just use it on your class property like that:
public class MyModel
{
[NotZero]
public double WhateverButNotZero { get; set; }
}