0

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.

  • 1
    https://stackoverflow.com/questions/3413715/how-to-create-custom-data-annotation-validators – Rand Random Feb 09 '22 at 18:14
  • 2
    IMO You shouldn't be altering the request, you should be validating it is correct, and if not throw a 400. The UI (or whatever is calling your API) should do the rounding. – Neil Feb 09 '22 at 18:19
  • you can set validation to allow number ending with 0 or 5 as multiple of 5 end in 0 or 5. https://stackoverflow.com/questions/16557754/perl-regex-to-find-any-number-that-is-a-multiple-of-5 – Rajesh G Feb 09 '22 at 18:26
  • You might need a custom validation attribute class that tests `cost % 5 == 0` in `IsValid()` method. Read about modulo operator in C#. – Rivo R. Feb 09 '22 at 18:29

3 Answers3

1

You can make cost variable as a prop and round there any given value by user till it is valid.

Fejbien
  • 11
  • 1
1

Try:

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

use the decimal instead of the int

haldo
  • 14,512
  • 5
  • 46
  • 52
Piero92
  • 38
  • 5
  • Where I should implement this class ? In Product Model class or in separate class ? – unknow_user Feb 09 '22 at 19:24
  • 1
    Your answer could be improved with additional supporting information. You could point to a reference stating why it is better to use decimal for money values. You could elaborate on how the Attribute has to be used. Instead of "use the decimal instead of the int" you should state that you mean the decimal type since not everybody knows that this even exists. – rominator007 Feb 09 '22 at 21:08
1

You can use a JS at the end of your view which is probably better as you will have a front end validation rather than posting the form before checking if it’s wrong (sorry I’m using my phone to type the suggested answer so may not be formatted properly) but something like this:

         <script>
                       document.getElementById("Submit_btn_id").addEventListener('click',function ()
                   {

                    //get the value entered in the input field 
                     var userEnterANumber = document.getElementById('input_id').value


                       if (userEnterANumber % 5 == 0)
                      {
                     console.write('This is a multiple of 5')
                         //turn off the required attribute from the input field 
                        document.getElementById("input_id").required = false;
                        }
   
                        else 
                        {
                          console.write('Not a multiple of 5')
                          //set the required attribute on the input field 
                        document.getElementById("input_id").required = true;
                          }
                        });
                </script>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jeff
  • 115
  • 7