-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"/>.
2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Syned, its working fine. But I have one more question related to session value. how to manage the session into generic handler class?
What do you mean "into generic handler class"? Describe please your use case.
Genric handler for http web request which is inherit the "IHttpHandler" interface.
Most of time you don't need handlers in your mvc application, why do you need it?
Genric handler use for fill drop down using jquery.
|
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.

2 Comments

Yes, you are right after refresh the page or single redirect TempData loose the data. But I thing session is not good way in MVC application
TempData uses session.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.