0

I am working on asp.net with mvc 4 architectute. Here i have two controller Display and SessionEx. In Display Controller i have an method like below

public ActionResult SessionExample()
{
    TempData["FortheFullRequest"] = "FortheFullRequest";

    string v = Session["Session1"].ToString();

    ViewData["Myval"] = "ControllertoView";

    ViewBag.MyVal = "ControllertoView";

    Session["Testing1"] = "Testing Session";

    return RedirectToAction("SomeOtherAction", "SessionEx");
}

In the SessionEx controller i have the method as below

public ActionResult SomeOtherAction()
{
    string str1 = Convert.ToString(Session["Testing1"]);
    string str2 = Convert.ToString(TempData["FortheFullRequest"]);

    return View();
}

I am debugging the project and i have also used watch to look at the gets stoted in tempdata and session. In the start the appropriate value in both session and tempdata but when the cursor reaches at RedirectToAction method all values gets stored in session and tempdata becomes null.Please help me someone here.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Sunny Sandeep
  • 971
  • 7
  • 18
  • 53

1 Answers1

0

If you want to store data that will be used after a redirect is made to another action method, then use Session.

TempData is mainly for one-time, short-lived, requests as discussed in this this SO question

You are likely finding that the data in TempData is not there after you have redirected to SomeOtherAction(), which is by desing with how TempData works.

To be honest, I never use TempData, I don't see the point myself.

Community
  • 1
  • 1
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • I have used session to store value but when the control passes to the destination the values becomes null. – Sunny Sandeep Aug 06 '15 at 13:31
  • When you execute this line `string str1 = Convert.ToString(Session["Testing1"]);` is the value always `NULL`? – Jason Evans Aug 06 '15 at 13:33
  • Yes when the control just passed from source controller the values becomes null. – Sunny Sandeep Aug 06 '15 at 13:36
  • That's odd. I don't know why that is the case. The one reason it owuld be `NULL` is if the session timeout and was restarted. But if you're getting `NULL` all the time, I'm not sure why that would be the case. Sorry. – Jason Evans Aug 06 '15 at 13:39
  • Even i am not understanding the reason why this is happening but i am facing this problem for whole day that when the destination controller comes the values is always null – Sunny Sandeep Aug 06 '15 at 13:43
  • I have solved it by own. Actually i have not used the used the namespace "using System.Web.SessionState;" which is required for controlling the session state. – Sunny Sandeep Aug 07 '15 at 06:34