I have a value on my model, that must fall within the range of two other values on my model.
For example:
public class RangeValidationSampleModel
{
int Value { get; set; }
int MinValue { get; set; }
int MaxValue { get; set; }
}
Of course, I can’t pass these Min/MaxValues into my DataAnnotations attributes, as they have to be constant values.
I’m sure I need to build my own validation attribute, but I haven’t done this much and can’t wrap my mind around how it should work.
I’ve searched for about an hour, and have seen all sorts of solutions for building custom validation, but can’t find anything to solve this particular problem using MVC3 unobtrusive validation.
You could write a custom validation attribute for this purpose:
and then decorate your view model with it:
then you could have a controller serving a view:
and a view of course:
Obviously the custom adapter registration should be performed in an external javascript file to avoid polluting the view but for the purpose and conciseness of this post I have put it inside the view.