I have some custom validation attribute classes in my model class that needs to be validated against a session object. How can this be achieved? In the model class, I cannot reference the current session object. Is there a way to somehow pass the session object into the model for validation? Or should I do this in the controller instead? And if I do it in the controller, how do pass back the error message back into the model class so that it shows up in my view page? This is for asp.net mvc 2.
thanks.
You can always
HttpContext.Current.Sessioninside your custom validation attribute (damn I already feel guilty for saying this). And if you don’t like this approach there’s the ModelValidator class in which you have the current HTTP context. Here’s a blog post illustrating the concept.