Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using TempData.Keep() save all prior TempData if using multiple Actions?

I asked this question yesterday.

I believe I have found the answer in using TempData. My new question is whether or not stringing along TempData through actions (as in wizard steps) will "keep" all the prior TempData or if I have to keep redeclaring to "keep" it in each action.

So, if TempData Step1 has fields 1, 2 and 3, and then Step2 has fields 4, 5 and 6, will 1 through 6 be kept in Step3 thusly:

[HttpPost]
public ActionResult Step1 (Step1Model model)
{
    if (ModelState.IsValid)
    {
        TempData["Step1"] = model;
        return RedirectToAction("Step2")
    }
    return View(model);
}

public ActionResult Step2 (Step2Model model)
{
    Step1Model step1 = (Step1Model)TempData["Step1"]
    if (step1 == null)
    {
        return RedirectToAction("Step1")
    }
    TempData.Keep("Step1")
    if (ModelState.IsValid)
    {
        TempData["Step2"] = model;
        return RedirectToAction("Step3")
    }
    return View(model);
}
public ActionResult Step3()
{
    TempData.Keep() // is this keeping Step1 and Step2?
    return View();
}

The above is oversimplified. Ultimately, I may have 6 or 7 steps and want to know if Step3 had to redeclare the use of Step1 and Step2 as I did in Step2 (i.e.: in a new Step3, would I have to write Step1Model step1...; and Step2Model step2...;?

like image 768
REMESQ Avatar asked Mar 05 '26 23:03

REMESQ


1 Answers

TempData items are only tagged for removal after they are read. When a key is marked for retention, the key is retained for the next request. As per MSDN TempData.Keep() Marks all keys in the dictionary for retention.

Just call TempData.Keep() before redirection

like image 168
Satpal Avatar answered Mar 08 '26 02:03

Satpal