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”?
My preference would be copy the data in your controller method to the
ViewDataor 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.