-2

I am using TempData[] in MVC application but its not working fine as like that session.

After refresh the page after login page then tempdata have null value please suggest me and also using in web.config

<sessionState mode="InProc" timeout="10"/>.
tereško
  • 58,060
  • 25
  • 98
  • 150

3 Answers3

1

TempData is available only for a user’s session, so it persists only till we have read it and gets cleared at the end of an HTTP Request. A scenario that fits the usage of TempData, is when data needs to persist between two requests – a redirect scenario. You can use method Keep to store until next request

   TempData.Keep

http://msdn.microsoft.com/en-us/library/ee703497.aspx

To fill data from controller, create action:

public ActionResult GetData()
{
   // get data from your data source, replace with db call or where to get data
   var data = new [] {"sample1", "sample2"};

   return Json(data, JsonRequestBehavior.AllowGet);
}

on client when you need data:

$.getJSON(@Url.Action("GetData"), function(data) {
   // fill dropdown instead alert
   alert(data);
});

See more:

AJAX request aspnet

similar question but for post

Community
  • 1
  • 1
syned
  • 2,201
  • 19
  • 22
0

It is supposed to be null after a refresh because TempData is meant only for a single redirect. In your case you must use Session instead.

Srinivas
  • 1,063
  • 7
  • 15
0

You can store your data in session.

for example

 public static int Points
        {
            get
            {
                int points = Convert.ToInt32( HttpContext.Current.Session["PointssessionKey"]); 
                return  points;
            }
            set
            {
                HttpContext.Current.Session["PointssessionKey"] = value;
            }
        }

and also keep in temp data as

TempData.Keep

MSTdev
  • 4,507
  • 2
  • 23
  • 40