views:

86

answers:

2

I recently ran into a situation in which accessing session information from view is the most intuitive and quick solution to the problem. So, I just did it without thinking too much. But now when I revisit my code. I somehow feel uncomfortable about the decision I made earlier. I can't clear state what is wrong of doing what I did. Just a "hinch" telling me I shouldn't do this.

Should I replace my code with ViewData or TempData? This will require adding a few lines of code. Or should I just leave things as is, because "it's not broken"?

+2  A: 

My preference would be copy the data in your controller method to the ViewData or the model itself. That really is more the responsibility of the controller, as opposed to the view.

I don't think it's officially considered a bad practice, but I could see it leading to maintenance headaches later on. You don't really want your views to be tied to external state; they should only be tied to the model/viewmodel/viewdata.

Aaronaught
Thanks for your suggestion. At least you confirmed that I am not a freak who wants to change code for no reason.
Wei Ma
+1  A: 

You're not a freak at all. In fact, I'm a militant refactorer when it comes to cleaning things up. For future reference, it's generally best practices to keep heavy logic out of your views. They say you're suppose to have very skinny/dumb views, controllers that do minor bits of logic and play traffic cop, and models that handle all the deep stuff.

I do everything in my power to limit my view logic to simple operations on the ViewModel and ViewData. I also use custom HtmlHelpers, RenderPartials, and RenderActions to keep my views skinny and uncomplicated.

DM