I'm building an MVC application. One of my taskS is to build a store. I set up a "wizard" like set of views that brings the user to fill different kind of data until the end of the operation, in total 7 steps.
My issue is about how to share some data between all these views.
First I used old-fashioned Session
and everything worked on my desktop, but when I finally deployed my application into my company's hosting server I got exceptions because Session
was erased randomly during some steps.
Now I modified everything to set up any data I need inside TempData
, and refreshing all data in each step and it's seems to work properly.
I'm a little confused!
My confusion is about all these structures: Session (I know it coming from asp.net), ViewData
, TempData
and the magic ViewBag
.
I read a lot about but I need someone that clearly tell me what is more appropriate for me in this case.
I'd say the ViewBag is perfect for something like this. Now, you're referring to the ViewBag as a "Magic" viewbag, but in reality it just wraps the ViewData which is a dictionary of <string,object>
The way the ViewBag works is that it's just a dynamic wrapper around the ViewData, so when you ask for something, let's say ViewBag.ShoppingCart, it basically ask the underlying dictionary if it has an entry called "ShoppingCart", and returns the item.
UPDATE Problem is I didn't remember that ViewBag and ViewData is view-specific, so they're emptied whenever you hit a different View/Action.
Unless you need the ShoppingCart (or wizard-progress) to be stored in a database, I'd go for ViewBag TempData in your case :)
You could also take a look at this article from Rachel Apple for a bit more info on the three:
http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
(Ps. I'd recommend reading the comments as well on that post to get some more unbiased opinions)