3

I am creating a site for a silent auction. My class has a CurrentBidAmount property, and a SuggestedBidAmount property. The SuggestedBidAmount is basically the current bid amount plus the current minimum bid increment. (So the current bid is $10, and the bid increment is $5, then the suggested bid is $15). When the bidder enters a new bid value, I want to validate that the new value is at least as much as the SuggestedBidAmount or greater.

I want to enforce this at the class level. The problem is, I'm not sure of what the validation attribute tags are, and can't seem to find them on Google. I must be using the wrong search terms, nonetheless, I can't find them.

The [Compare] tag is close, but that does a verbatim compare. I basically need to compare one property against another, and verify that it is equal to or greater than the other property.

Can anyone please point me in the right direction?

1 Answer 1

2

You can create your own custom validation attribute and just override its IsValid method:

    [AttributeUsage(AttributeTargets.Class)]
    public class BidCompareAttribute : ValidationAttribute
    {
        public string CurrentBidPropertyName { get; set; }
        public string MinBidIncrementPropertyName { get; set; }

        public override bool IsValid(object value)
        {
            var properties = TypeDescriptor.GetProperties(value);
            var minBidIncrement = properties.Find("MinBidIncrement", true).GetValue(value) as IComparable;
            var currentBid = properties.Find("CurrentBid", true).GetValue(value) as IComparable;
            return currentBid.CompareTo(minBidIncrement) >= 0;
        }
    }

Than use it like this:

    [BidCompare(CurrentBidPropertyName = "CurrentBid", 
                MinBidIncrementPropertyName = "MinBidIncrement")]
    public class BidModel
    {
        public int CurrentBid { get; set; }    
        public int MinBidIncrement { get; set; }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Frennky. I tried the method you just listed, and I also tried having the model implement IValidatableObject and overriding the 'public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)'method. In both cases, I have a weird issue. The problem is that the value that I typed into the text field in the web (which is 'bound' to the CurrentBid property via Razor syntax) is NOT the value that is coming through to test. I am getting the value PRIOR to pressing submit. Which will of course, always be the wrong value. Any ideas?
I'm not sure I can be of much help without more insight in your code. All I can say is to double check the code that binds data to model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.