views:

160

answers:

2

I know this is probably not possible but let's say I have a model with two properties.

I write a ValidationAttribute for one of the properties. Can that VA look at the other property and make a decision?

So;

public class QuickQuote
{
    public String state { get; set; }

    [MyRequiredValidator(ErrorMessage = "Error msg")]
    public String familyType { get; set; }

So in the above example, can the validator test to see what's in the "state" property and take that into consideration when validating "familyType"?

I know I can probably save the object to the session but would like to avoid any saving of state if possible.

+1  A: 

Another way to achieve this kind of validation is to have your model implement IDataErrorInfo. That way you can do whole viewmodel validation.

This page has some information about iplementing the IDataErrorInfo Interface, about 2/3 of the way down under the heading "mplementing the IDataErrorInfo Interface"

Mac
+1, this may be what I'm looking for. Reading it now thank you.
griegs
A: 

Your custom validation could be applied to the class directly, take a look at PropertiesMustMatch attribute in the AccountModels class that is created by default as a part of the MVC project template in VS2008.

bhiku