Transfer data from action to another action

How do you transfer a model from an action (GetDate) to another (ProcessP) using the RedirectAction method?

Here is the source code:

[HttpPost] public ActionResult GetDate(FormCollection values, DateParameter newDateParameter) { if (ModelState.IsValid) { return RedirectToAction("ProcessP"); } else { return View(newDateParameter); } } public ActionResult ProcessP() { //Access the model from GetDate here?? var model = (from p in _db.blah orderby p.CreateDate descending select p).Take(10); return View(model); } 
+4
source share
1 answer

If you need to transfer data from one action to another, you need to use TempData . For example, in GetDate, you can add data to a session as follows:

 TempData["Key"] = YourData 

And then redirect. In ProcessP, you can access data using a previously used key:

 var whatever = TempData["Key"]; 

For a decent read, I would recommend reading this thread: ASP.NET MVC - TempData - Good or Bad Practice

+7
source

All Articles