开发者

How to validate in model object with attribute against a session object?

开发者 https://www.devze.com 2023-02-07 02:33 出处:网络
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 sess

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消