TempData 可以工作,但被一个请求延迟

发布于 2024-12-18 01:27:16 字数 434 浏览 5 评论 0原文

我正在使用 TempData 在发布后通过重定向来携带消息。控制器设置临时数据如下所示:

TempData["message"]="foo";
return RedirectToAction("Index");

在 _Layout.cshtml 中,我有以下片段:

@{var temp = TempData["message"] as string; }
@if ( temp != null)
{
     <div class="message">@temp</div>
}

我现在的问题是,重定向后,消息不显示。但是,在紧随重定向(刷新或任何其他页面)之后的请求中,将显示该消息。显示后,它会按预期从会话中删除。

如何使我的 TempData 显示在我重定向到的页面上?

I'm using TempData to carry messages with Redirect after Post. The controller sets the tempdata as shown here:

TempData["message"]="foo";
return RedirectToAction("Index");

In the _Layout.cshtml, I have the following fragment:

@{var temp = TempData["message"] as string; }
@if ( temp != null)
{
     <div class="message">@temp</div>
}

My problem is now, that after the redirect, the message is not displayed. However, on the request that follows immediately the redirect (refresh or any other page), the message is displayed. After being displayed, it is removed from the session as expected.

How can I make my TempData display on the page I redirect to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-12-25 01:27:16

你需要使用

TempData.Keep(key);

You need to use

TempData.Keep(key);
Saygoodbye 2024-12-25 01:27:16

当您这样做时:

TempData["message"] = "foo";
return RedirectToAction("Index");

当您重定向到的索引页面呈现其视图时,将显示该消息。

When you do:

TempData["message"] = "foo";
return RedirectToAction("Index");

The message will be displayed when the Index page to which you are redirecting renders its view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文