【发布时间】:2021-10-06 22:03:02
【问题描述】:
我在 Redirect 后遇到了 TempData 问题。
public ActionResult LoginCredentials()
{
// Calling "SetError()" in catch(), if password mismatch.
try{}
catch()
{
return SetError();
}
}
public ActionResult SetError()
{
// set the value of TempData as "true"
TempData["error"] = true;
return Redirect("/Login");
}
public ActionResult Index()
{
ViewData["useError"]= TempData["error"]; // at this point TempData["error"] is null.
...
}
在SetError()中TempData的值成功设置为真,“重定向”后出现问题,值变为“空”,我不能再使用了。
【问题讨论】:
-
请在此处发布您的控制器类代码...
-
无论我有什么我都发布了。
-
(Index) 函数中的 TempData["error"] 将为空,因为 (SetError) 函数未执行。
-
很难理解你想在这里做什么。应该是
return RedirectToAction("SetError);而不是return SetError();,但是该方法所做的只是再次重定向,那么为什么不在LoginCredentials()方法中只使用catch() { TempData["error"] = true; return RedirectToAction("Index); }呢? -
@user3106445 我尝试完全按照您的做法做,对我来说效果很好,问题可能是别的。
标签: c# asp.net-mvc