TempData works, but is delayed by one request

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?


你需要使用

TempData.Keep(key);

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.

链接地址: http://www.djcxy.com/p/74432.html

上一篇: 在课堂中使用Tempdata

下一篇: TempData有效,但延迟了一个请求