I have an asp.net mvc3 w/ ado.net entity framework doing some validation.
I have created a viewmodel as such
public class id
{
[Required]
public decimal l_ID
{
get;
set;
}
[Required]
public decimal v_ID
{
get;
set;
}
}
Is it possible to add some set of validation rules so that the l_id must be larger than the v_id? validation should be done once the user has submitted the page. How would this be done? Any tutorials? Does this validation need to be done in the controller or using partial classes? Is there any examples out there
I’ve been using the IValidatable interface, its fairly simple compared to custom attribute validation. Here’s the code:
Couple of notes, the Validate method is called automatically when the binding occurs from a POST action that takes the ViewModel as a parameter, so you don’t have to do any wiring up of events. The
bool _hasBeenValidatedthing is in there because right now there’s a quasi bug in MVC3 (imho) that calls that validation method twice in some cases (like when this ViewModel is also used as a member of another ViewModel and that gets posted)The second param of the ValidationResult constuctor is the name of the property that the validation is bound to, so in this case, your ValidatorFor tag for the l_ID in your View would get that “Bad thing” message in it.