[SOLVED] Cost must be multiple by 5

Issue

I am working small application with basic CRUD operation and I have Product Model.
In this model I have something like

public class Product  
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int amountAvailable { get; set; }
        public double cost { get; set; }

        [ForeignKey(nameof(User))]
        public int UserId { get; set; }
        public User Users { get; set; }
        
    }

Right now, I need some shortcut or some hack how to make cost (cost of the product), but it should be a multiple of 5, meaning the price can be 5,10,15,20,25,30… etc.
Is there anything which I can force user to put price something like thiss?
I try with [DataAnnotation] and using Range but I think this will not work.

Solution

Try:

public class MultipleOf5Attribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return ((int)value) % 5 == 0;
    }
}

use the decimal instead of the int

Answered By – Piero92

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *