1

What I am trying to do is to save a List into TempData, that I'm later casting back to its type and passing to a view but the TempData after returning from a controller loses its data.

Controller A:

public ActionResult Index(int? Page, int? id, int? attId, int? EnrollNumber, int? attend, DateTime? reqDate, DateTime? reqDT, DateTime? reqTime, DateTime? StartDate, string deets = " ",  string preAppr = "", string type = "")
{

//some code

new UserInfoController().reLabelLogs(reqTime.Value.Date, reqTime.Value, id, 0, 0, null, attLogs);
var atLogs = new List<AttendanceLog>();
var temp = TempData["ppList"]; //its empty?
atLogs = (List<AttendanceLog>)TempData["ppList"];
return View(atLogs);
}   

Controller UserInfo:

public void reLabelLogs(DateTime? startDate, DateTime? startDateTime, int? empId, int? isPending, int? isManual, DateTime? manualDate
            , List<AttendanceLog> pList)
{

//some code
 if (pList == null)
            {
                  data = db.AttendanceLogs
                    .Where(z => z.EmpID == empId && z.Date >= startDate && z.Date <= today).ToList();
            }
            else
            {
                  noSave = true;
                  data = pList.ToList();
            }
foreach (var log in data)
 {
 // some code
 }
   TempData["ppList"] = data; //there is data but soon after the control goes back to Controller A it becomes null
}

What did I try?

I tried using:

Session (but it would give NullException at UserInfoController)

ViewBag.ppList (same result as TempData)

Tried TempData.Keep(); and TempData.Peek();  (no help)

enter image description here

  • I think you need to pass the context to the UserInfoController – J.Loscos May 17 '18 at 14:44
  • Session[] should work, make sure you are correctly replacing it on every place. – EzLo May 17 '18 at 14:45
  • added the picture with session approach –  May 17 '18 at 14:45
  • You can't test this by creating a controller per se, you need the actual framework to build it (the context will be null, as your session). Read this post https://stackoverflow.com/questions/889516/session-null-in-asp-net-mvc-controller-constructors – EzLo May 17 '18 at 14:47
  • 1
    you are using controllers incorrectly. You should never be instantiating controllers from other controllers. if you need data in two places, refactor your code to use a common service and call the common service from both. – Fran May 17 '18 at 15:22

4 Answers4

1

TempData and Session doesn't work because your UserInfoController doesn't have a context. You could try passing it the current context :

var controller = new UserInfoController()
controller.Initialize(new RequestContext(this.HttpContext, this.RouteData));

controller.reLabelLogs(reqTime.Value.Date, reqTime.Value, id, 0, 0, null, attLogs);

Or you could use the session by getting the current http context:

HttpContext.Current.Session["list"] = data;
J.Loscos
  • 2,478
  • 1
  • 10
  • 17
0

The answer is simple:

TempData is a session-backed temporary storage dictionary that is available for one single request. It’s great to pass messages between controllers.

You have to use something different, like Session. We needs to know what kind of problem are you facing using Session (if I understand you have already tried this solution)?

Roberto Conte Rosito
  • 2,080
  • 12
  • 22
  • Okay but how do I use the session then? I tried with `Session["pList"] = data;` but it failed –  May 17 '18 at 14:41
  • When you execute the cast, your resulting object is null? The page "Index" is called before or after the controller UserInfo? The cast can returns null if the execution is not ordered simple because the object not exists yet. – Roberto Conte Rosito May 17 '18 at 14:44
  • no no not when casting, its null when storing please check the edite ques –  May 17 '18 at 14:46
  • Can you post somewhere the entire controller file? There is something really strange. The Session object is related to the MVC framework and should be not null if you are executing an HTTP request. – Roberto Conte Rosito May 17 '18 at 14:48
0

Tempdata is behaving as it is designed: read more here - Using Tempdata in ASP.NET MVC - Best practice

Session however could work, so maybe you did something wrong there

sander
  • 719
  • 2
  • 9
  • 21
  • Okay but how do I use the session then? I tried with `Session["pList"] = data;` but it failed –  May 17 '18 at 14:41
  • https://stackoverflow.com/questions/14138872/how-to-use-sessions-in-an-asp-net-mvc-4-application - try it like this – sander May 17 '18 at 14:43
  • that's exactly how I did –  May 17 '18 at 14:44
0

In order to access session variables in secondary controller you should use

 System.Web.HttpContext.Current.Session["ppList"] = data;
DirtyBit
  • 16,613
  • 4
  • 34
  • 55