How to validate in model object with attribute against a session object?
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.Session
inside 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.
精彩评论