4

İn my MVC3 project, there is plenty of TempData[] that I am using for passing datas between actions. And it works totaly perfect when I use Chrome. But in IE I can't get values of TempData[] items. if anyone knows whats the problem and how can I solve it?`

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        TempData["id"] = "someData";
        return View();

    }
}


public class AnotherController : Controller
{
    public ActionResult AnotherAction()
    {
        string data = Convert.ToString(TempData["id"]);
        return View();

    }
}

`

serhads
  • 462
  • 2
  • 7
  • 22
  • 2
    It's not supposed to be a browser issue. This is solely rendered on `server` and you should take special consideration on your `View`. Feel free to provide your View. That might help people to get into the problem – Abdul Munim Oct 26 '11 at 11:34
  • 1
    TempData persists only for the very next request, so use fiddler or some other http profiler to see if there is a difference between the requests coming from chrome and IE. – Xhalent Oct 26 '11 at 11:38
  • @Munim Thank you, you're right it shouldn't be a browser issue, but happens.by the way in views ı dont use anything related with tempdata. from one action to other, the page goes with so thats why ı couldn find reasonable explanation for it. – serhads Oct 26 '11 at 12:04
  • @Xhalent Thank you, I know TempData only for only very next request. but in my case, even if it's very next request or not I have a problem in IE however in Chrome suprisingly it works even after a dozen request. Its very wierd. – serhads Oct 26 '11 at 12:05
  • I would agree with @Xhalent, take a look at Fiddler for any unwanted request are performed from IE. – Abdul Munim Oct 26 '11 at 12:20
  • @Xhalent As I found out very recently, since MVC 2, a TempData value will only be removed _after_ it has been read. See [here](http://weblogs.asp.net/jacqueseloff/archive/2009/11/17/tempdata-improvements.aspx) for details. – John H Oct 26 '11 at 13:27
  • @serhads Can you post your views? – John H Oct 26 '11 at 13:34

2 Answers2

5

You should never return a view from a controller action that stores something into TempData. You should immediately redirect to the controller action that is supposed to use it:

public class SomeController : Controller
{
    public ActionResult SomeAction()
    {
        TempData["id"] = "someData";
        return Redirect("AnotherAction", "Another");
    }
}


public class AnotherController : Controller
{
    public ActionResult AnotherAction()
    {
        string data = Convert.ToString(TempData["id"]);
        return View();
    }
}

The reason for this is that TempData survives only for a single additional request. So for example if inside the view you are sending an AJAX request to some controller action (no matter which) and then have a link in this view pointing to the target action, when the user is redirected to this target action TempData will no longer exist since it was lost during the AJAX request done previously.

If you need to store data for longer than a single redirect you could use Session.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

If you need to store data for longer than a single redirect you should use Keep or Peek methods.

string data = TempData["id"].;
TempData.Keep("id");

or simply use,

string data = TempData.Peek("id").ToString();

Peek function helps to read as well as advice MVC to maintain “TempData” for the subsequent request.

Chinthaka
  • 343
  • 3
  • 13