What is wrong with having a private ViewModel object, so that all my controller actions have access to it?
-
I’m using EF4,MVC3,DBContext, DBsets.
public class MyController { private MyViewModel _myViewModel; public ActionResult Index(MyViewModel myViewModel){ <-- There is a model Binder making this work _myViewModel = myViewModel; return _myViewModel; } }
Because everytime you call a controller action you get a different instance of the controller. So anything that you might have stored in instance fields of this controller from a previous action would be lost on subsequent actions. That’s the reason why in ASP.NET MVC you have notions such as Session, Application State, TempData, Cookies, Cache, … you name it.