Reputation: 351
I am facing issues with TempData after Redirect.
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.
...
}
In SetError() value of TempData is successfully set as true, issue takes place after "Redirect", value becomes "null" and I can't use it anymore.
Upvotes: 8
Views: 16841
Reputation: 11
Setting the cookie as essential in CookieTempDataProviderOptions
Solved the issue for me.
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = _ => true;
options.MinimumSameSitePolicy = SameSiteMode.Strict;
});
services.Configure<CookieTempDataProviderOptions>(options =>
{
options.Cookie.IsEssential = true;
});
Upvotes: 1
Reputation: 49
I find .Net Core incredibly problematic. I had to turn this off in the Configuration
options.CheckConsentNeeded = context => true;
and it worked when I used Redirect to navigate to another page.
However on refreshing the page the TempDate or ViewData lose their value. But when I reassigned it to itself in the "View" it worked:
@{
TempData["somevalue"] = TempData["somevalue"];
}
Upvotes: 2
Reputation: 435
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
//options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
turn off options.CheckConsentNeeded = context => true;
this worked for me
Upvotes: 7
Reputation: 370
Upvotes: 5
Reputation: 1492
Use RedirectToAction
public ActionResult SetError()
{
// set the value of TempData as "true"
TempData["error"] = true;
return RedirectToAction("YourViewName");
}
Upvotes: 1
Reputation: 1120
As far as i understand ViewData
saves data only after redirect, not when just another Http request occur. So inside Login
method(where you redirect to) this ViewData["useError"]
have to be available, but Index
method is just another method which is executed during another http request.That's why ViewData["useError"]
is empty
You can use Session if you want to store data between different Http requests
Upvotes: 0