Proper use of TempData in ASP.NET MVC3?

I have an ASP.NET MVC3 application where my action generates a list of identifiers that I want to make available for a subsequent AJAX request. This means that I can run a long process in the background and poll it. The list of identifiers is a necessary contribution to this lengthy process. I do not want to pass them to the URL as a parameter, because the list can be very long and cause problems in IE.

My controller

public ActionResult Run() { List<MyObjs> objs = _db.MyObjs.ToList<MyObjs>(); string uniqueId = Guid.NewGuid().ToString(); ViewData["UniqueID"] = uniqueId; TempData["ObjIdList" + uniqueId] = String.Join(",", objs .Select(o => o.ObjID).ToArray<int>()); return View(objs); } public void StartProcess(string uid) { string ids = TempData["ObjIdList" + id].ToString().Split(','); ... } 

My view

 var uniqueId = '@ViewData["UniqueID"]'; $(document).ready(function (event) { $('#startProcess').click(function () { $.post("/Scheduler/StartProcess", { uid: uniqueId }, function () { getStatus(); }); event.preventDefault; }); }); function getStatus() { var r = new Date().getTime(); // cache killer for IE var url = '/Scheduler/GetCurrentProgress/' + uniqueId + "?r=" + r; $.get(url, function (data) { if (data != "100") { $('#status').html(data); setTimeout(function () { getStatus(); }, 100); } else { $('#status').html("Done"); }; }); } 

This works in my internal test, albeit on my laptop with one simultaneous user. Is it safe or is there a better way to transfer this data?

+7
source share
3 answers

Brandon

TempData is similar to ViewData, except that it is saved for two consecutive requests, which makes it useful for things like transferring data between two different controller actions.

Jason s

TempData in MVC is actually saved until received. Since the FYI Tempdata is actually stored in a custom SessionState, so it looks more like SessionData than ViewData p>

Taken from one of my answers to the questions - Result of the MVC3 controller action "Remember" Elapsed time identifier

Essentially, TempData is similar to the session property - (stored in SessionState) used to communicate between two consecutive controller requests. As if this is a good or bad practice in your case, I think it would be nice to pass data to tempdata, but these are other options, hidden fields among them. Another good reference for viewing is ASP.NET MVC - TempData - Good or Bad Practice

+10
source

TempData lifetime TempData very short. From the current request to the subsequent request. TempData uses Session to store data behind the scenes. But the lifetime is shorter than the regular session variable, that is, until the next request.

If you are sure that you are going to make an ajax call right after the previous call, where you install TempData, you can use this. If you want more control, you can save it in the Session variable and destroy the session variable after using it where you wanted n times.

+3
source

Use TempData only when you want to transfer values ​​between controllers in ASP.NET MVC.

0
source

All Articles