0

How can I keep some values between different Views in MVC 3?

I tried using TempData and HiddenField to keep these values but in our hosting this tecnique seems to have short life so exceptions are coming out furthermore if user uses Back button every starts to fail.

I would like to understand the better way to keep values between views in MVC 3, thanks in advice!

JasonMenny
  • 223
  • 4
  • 14

3 Answers3

3

By design, MVC3 TempData values are removed after they are used.

The most simple answer is to use the Session object directly in your controllers.

There are other related questions with detailed answers such as these:

Session variables in ASP.NET MVC

Asp.Net MVC and Session

Community
  • 1
  • 1
Jonathan S.
  • 5,837
  • 8
  • 44
  • 63
  • Is it good approach to use sessions as usage of session is kind of resource consumptions? – Hari Gillala Sep 22 '11 at 15:30
  • There are trade-offs when deciding between using the session or cookies or a database for persistence that are too in-depth for a comment :) See the other similar questions for ideas. Also, if I'm using the Session, I don't call the Session object directly. There are practices that can be used to wrap the session so it's more robust. – Jonathan S. Sep 22 '11 at 15:37
  • 1
    @StewieFG Session state usage isn't inherently resource intensive unless you start throwing large amounts of data in it. If you are you have a fundamental design issue anyway. It only becomes a concern when you move to a server farm environment. Ideally you should aim for a stateless design whenever possible but if you do use it always provide an abstraction within your code to allow for support of alternative storage implementations. – Darren Lewis Sep 22 '11 at 15:42
  • @Jonathan S: why it's not a good thing to call Session Object directly? I came from Asp.net where this practice wasn't bad. By the way I have to store very simple data, a couple of string and then a simple object with few properties, I think performance will not affected. – JasonMenny Sep 23 '11 at 07:13
  • Jason - there's no problem calling the session object directly for simple usages. It's when it becomes more complex that you might want to have some type of wrapper to manage your sessions. – Jonathan S. Sep 23 '11 at 13:29
0

Your question is about the lifecycle of objects in between requests. It's important to understand that webapplications are used over the HTTP(S) protocol which is a stateless protocol. This means that every request is a completely new request for the webserver and there's no state shared between requests.

However it would be foolish to send the credentials of a user to the server each and every time so a webserver can create a thing they call a Session (and session-state). This object is an object that remains available for the lifetime of the session of the current user (most of the times from logging in until logging out). You can use this object to store items that you wish to share over various requests of the same user.

If the values you're trying to keep are specific to the page you can probably use a hidden field or something like that. However if the data is more related to the user than to a specific page and it must have a lifecycle longer than a single request then sessionstate is the best place to store the data.

thekip
  • 3,660
  • 2
  • 21
  • 41
0

You could use the Session (as you mention in your title and tags). Or store a cookie on the user's machine

GvS
  • 52,015
  • 16
  • 101
  • 139
  • I used Session, I had 10 steps to complete my procedure (10 Views logically linked) but somehow Session["myparameter"] goes null in a certain point! Can you tell me why? No Session.Clear() is called! – JasonMenny Sep 23 '11 at 08:53
  • Is your site distributed over different servers? – GvS Sep 23 '11 at 09:00
  • No it's hosted on a single server, but Im figuring out with hosting customer care that maybe the problem could be some IIS 6.0 settins, I read different resources about this issue, Im waiting thier response because on my machine everything is running well. – JasonMenny Sep 23 '11 at 09:27