在catch块中的RedirectToAction之后,TempData为空.可能是什么原因?

Dmy*_*tro 5 c# asp.net-mvc tempdata

我一直在使用TempData很长时间,并且面临着一些奇怪的问题.我有基本情景:

[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
    ...
}
Run Code Online (Sandbox Code Playgroud)

上传失败并return RedirectToAction("Edit", newProduct.Id);执行行时会发生此问题.丢失临时数据值的原因可能不是很明显?

更新: 当我使用时

TempData["error"] = "error";
RedirectToAction(...);
Run Code Online (Sandbox Code Playgroud)

catch块之外一切正常,临时数据值转移到Edit动作.

Ish*_*iaq 0

看来问题出在return RedirectToAction("Edit", newProduct.Id);声明上。
尝试这个语句,而不是return RedirectToAction("Edit", new{Id=newProduct.Id});
路由参数是object类型并且您传递int.