ASP.NET MVC - Data Transfer Through Views

I am creating an MVC application. One of my tasks is to build a store. I configured the "master" as a set of views that forces the user to fill in different types of data until the end of the operation, in just 7 steps.

My problem is how to share some data between all of these views.

At first I used the old-fashioned Session , and everything worked on my desktop, but when I finally deployed my application to my hosting server, I got exceptions because Session was deleted randomly in a few steps.

Now I changed everything to configure any data that I need inside TempData , and update all the data at each step and it seems to work correctly.

I'm a little confused!

My confusion concerns all of these structures: the session (I know this comes from asp.net), ViewData , TempData and the magic of ViewBag .

I read a lot, but I need someone who will clearly tell me what suits me better in this case.

+8
asp.net-mvc wizard
source share
3 answers

I would say that the ViewBag is perfect for something like that. Now you mean the ViewBag as a “Magic” viewbag, but actually it just terminates the ViewData, which is the <string,object> dictionary

The way the ViewBag works is just a dynamic wrapper around ViewData, so when you ask for something, say, ViewBag.ShoppingCart, it basically asks for a basic dictionary if it has an entry called "ShoppingCart" and returns the item.

UPDATE The problem is that I don’t remember that the ViewBag and ViewData are view dependent, so they are cleared whenever you click on another view / action.

If you do not need ShoppingCart (or master progress) to be stored in the database, I would go to ViewBag TempData in your case :)

You can also take a look at this article from Rachel Apple for more information on these three:

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

(Ps. I would also recommend reading the comments on this post to get more objective opinions)

+3
source share

There is nothing wrong with using hidden fields - at least in my book.

I would "fix" the problem of the session instead of writing code around the problem. Run a simple test: change the session provider to SQL, disable the hidden fields and see if your application is working correctly. If this does not happen, there are other (big) problems that you need to worry about.

Is this application intended to run in a web farm / cloud / behind a load balancer? If so, you should either:

  • change the session provider to something else: SQL, StateServer, memcache, etc. Not a lot of code changes are required.

http://msdn.microsoft.com/en-us/library/ms178587.aspx and http://memcachedproviders.codeplex.com/

OR

  • recycle your wizard steps and reduce the dependency on shared values ​​between views. Session ID is all you need and you can query the DB for something else. Not very fast, but safe and stable.

Optimization: use as many hidden fields as you want to reduce the number of database queries (for example, I said nothing wrong with that), but usually one field is enough to maintain a serialized state between queries: http://blog.maartenballiauw.be/post/ 2009/10/08 / Leveraging-ASPNET-MVC-2-futures-ViewState.aspx .

Even if you don’t need to worry about multiple instances of your application (on different computers), IIS processes workflows from time to time. When this happens, you can end up running two instances at the same time (for short periods of time) on one computer, and some of your users may be unsuccessful to get to the server at precisely these moments.

It doesn’t matter if the next request falls into another instance of your application. Always try to develop for this purpose.

Hope this helps!

+2
source share

You have several options: Use a session, viewdata (or viewbag), but you need to transfer it using hidden fields or cookies.

Viewdata has a problem that will give more work.

I would go with a session, but maybe it will be cleared in your case, because you probably have more than one server, and when the second request hits another server, it simply will not have data from the previous step.

Solve this problem by using a server that contains a session for all servers or uses cookies (if the information is NOT critical).

0
source share

All Articles