2

I have a class with a property which has a range attribution on it.

        [Required]
        [Range(5, 9999)]
        public double Price { get; set; }

Which is respected at all times and on any user input.

But on one specific code path, where i am automating some entries, i want to be able to set the Price to 0. So i want to programmatically tell this model class to ignore this requirement.

Is it possible? If so how?

MoXplod
  • 3,813
  • 6
  • 36
  • 46

2 Answers2

0

This seems like a duplicate of this question, but maybe I'm not completely understanding your requirements.

For your scenario I might try implementing the IValidatableObject interface so that way you can control the validation logic rather than using a declarative validation pattern.

Community
  • 1
  • 1
Sneal
  • 2,546
  • 20
  • 22
  • i am not using views in many cases. Some times these models are called using API's or windows services etc. – MoXplod Jan 02 '12 at 05:50
0

I would suggest you to create a derived type of System.ComponentModel.DataAnnotations.ValidationAttribute. The implementation can be similar to RangeAttribute class with additional parameter of exceptional values.

Have a look at this MSDN article for details on how to do this.

Maheep
  • 5,539
  • 3
  • 28
  • 47