0

As I have read that once you set a tempdata, it will last till next request and if you need to use it more then we need to use keep. But in this situation there is somewhere my tempdata is being lost.

The scenario is below:

I have a view and corresponding to that I have a action method in my controller and here I just set a tempdata as below:

Controller class:

public actionresult myview()
{
   tempdata["Empid"]= sourceid;
} 

The view consists of several renderAction as below and all these actionmethods in controller return some partialviews

@html.renderAction("details","mycontroller")
@html.renderAction("details","mycontroller")
@html.renderAction("details","mycontroller")
@html.renderAction("details","mycontroller")

Now in partialviews, I have several ajaxified calls suppose in one of the partialview , I have a post method using ajax like below:

$.post("action", "controller",{}});

Please ignore syntactical mistakes as I only have problem in understating the logic of tempdata.

So, coming to above, now the post methods have actionmethods that uses the tempdata that i have set when my view page loads i.e tempdata["Empid"] because this tempdata is needed to get details of employee.

So, it gets the data, reurn json formatted data and I show it in view perfectly. Uptill here everything is fine. Now, I have one of the partialviews that uses the ajax post to hit controller which is not the same controller but a different cs file. Here this tempdata goes off.

Why is it happening so..

1 Answer 1

1

TempData is designed for a short life by default. So the data you set to TempDataDictionary object persists only from one request to the next request. After that it is not going to be available.

You can use the TempDataDictionary.Keep() method to persist the value for the next request again.

public ActionResult Index()
{
  TempData["Message"]="Hello";
  return View();
}
public ActionResult GetCustomer()
{
  var msg = TempData["Message"] as string;
  TempData.Keep("Message");
  return View();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Absolutely right Sir.Just a case if I set a tempdata in controller in action method that returns a view and I did not used that tempdata. Instead my view have some render actions which are returning partial views and in those partial views I have ajax calls which uses the tempdata. Will the tempdata still persists in my ajax call. I am asking you because actually it shouldn't persist but in this case tempdata persists in all my ajax requests. Each partial view has ajax calls and I am using the same tempdata without using keep. That means render actions are not new requests?
Why don't you try it yourself and see ? You might also consider session state or a db table if you think tempdata is too much trouble.

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.