1

Both TempData[] and static fields can be used to pass data from ActionResult to another.

Is there performance/memory difference? best practice?

I also noticed that when paging or sorting, the Detail function is recalled which lead that the list of cars should remain in memory.

public class TestController
 {
      private static IEnumerable<Cars> _cars;

      public ActionResult Detials()
      {
          var uploadedCars = TempData["cars"] as IEnumerable<Cars>;
          var testViewModel = new TestViewModel();
          var result = TestViewModel.Process(uploadedCars);
          //var result = TestViewModel.Process(_cars);
          return View(result);
      }

      public ActionResult UploadCars(object obj)
      {
          // upload file ...
            _cars= null; // reset value
            //_cars= loader.GetAllCars(uploader);
            TempData["cars"] = loader.GetAllCars(uploader);
            return RedirectToAction("Detials");
        }
      }

 }
Maro
  • 2,579
  • 9
  • 42
  • 79
  • `TempData` seems a lot safer, as a `static` field could be over-written by two users accessing the page simultaneously. – David Apr 13 '16 at 13:53
  • but when i use TempData and in case of sorting or filtering, the TempData is cleared – Maro Apr 13 '16 at 13:55
  • 1
    `TempData` gets cleared after it's been consumed. If you need the data to persist longer than that then you can persist it somewhere, such as session state or perhaps as part of the request/response itself (the latter being a more "RESTful" approach). – David Apr 13 '16 at 13:57
  • This might help http://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction/11209320#11209320 – Shyju Apr 13 '16 at 13:57

0 Answers0