5

I've been using TempData for a long time and faced strange issue for me. I have basic scenario:

[HttpPost]
public ActionResult Create(ProductCreateModel newProduct)
{
    // create and save product to db

    // try upload product to external site
    try { UploadProductToEbay(newProduct); } 
    catch { 
              TempData["error"] = "error";
              return RedirectToAction("Edit", newProduct.Id);
    }
    ...
}

[HttpGet]
public ActionResult Edit(int Id)
{
    var error = TempData["error"]; // at this point temp data collection is empty and have no idea why
    ...
}

The issue takes place when upload fails and return RedirectToAction("Edit", newProduct.Id); line is executed. What could be not very obvious reasons of losing temp data values?

UPDATE: When I use

TempData["error"] = "error";
RedirectToAction(...);

outside the catch block everything works fine, temp data value is transfered to Edit action.

Dmytro
  • 16,668
  • 27
  • 80
  • 130

1 Answers1

0

It seems the problem is in return RedirectToAction("Edit", newProduct.Id); statement.
try this statement instead return RedirectToAction("Edit", new{Id=newProduct.Id});
Route Parameter is of object type and you passing the int.

Ishtiaq
  • 980
  • 2
  • 6
  • 21
  • This is not the issue, redirect works fine, this is not the actual code, I really use new{Id=newProduct.Id} but the problem is temp data related somehow. – Dmytro Apr 22 '14 at 12:12
  • Okay, You are redirecting from a POST action to GET action, May be this will creating the problem. Me not sure about this.:) – Ishtiaq Apr 22 '14 at 12:23
  • I've written in update to question that issue takes place only when TempData value assignment and redirect are called in catch block. Otherwise everything works like expected. – Dmytro Apr 22 '14 at 12:38