private member in controller returns null after being set in action result

I have a controller in ASP.NET MVC application.

private GipDbContext db = new GipDbContext();
private Employee employeeObj;

public ActionResult Edit(int? id)
            Employee employee = db.Employees.Find(id);

            //employeeObj SET TO ANOTHER OBJECT
            employeeObj = employee;

            return View(employee);

public PartialViewResult TimeSeriesData(int? tsdataid)
            TimeSeriesData tsobject = new TimeSeriesData();

            // employeeObj RETURNING NULL
            foreach (var item in employeeObj.TimeSeriesData){
                if (item.TimeSeriesDataID == tsdataid)
                    tsobject = item;

The first method being called is Edit, then when TimeSeriesData is called employeeObj is returning null, even though it was set in the Edit method.. any ideas why?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Controllers are instantiated for each request. Once you request TimeSeriesData, the controller that was created for the request to Edit has already been disposed. If you need to hold on to a piece of data between requests, you must either put it in Session or TempData (both of which use sessions).

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply