Posts TempData is always null
Post
Cancel

TempData is always null

I was using TempData for the submission for the data and send mail. Then pass through the TempData to HTML whether the form submission and mail send was successful or not.

To show the message I use toastr for showing the message.

This is the CDN for using toastr. Which I used.

1
2
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>

This is the JS code to show the toastr.

1
toastr.success('Successfully message.');

But I was not getting the TempData. Instead it was always null. So I researched and tried many options.

There are 2 solutions

Later I found one stackoverflow post. which gave me the solution. Add below code to Startup.cs

1
2
3
4
5
// The Tempdata provider cookie is not essential. Make it essential
// so Tempdata is functional when tracking is disabled.
services.Configure<CookieTempDataProviderOptions>(options => {
      options.Cookie.IsEssential = true;
});

So we need to make the Cookie as essential.

2. Accept the message

Add this to your page.

1
<partial name="_CookieConsentPartial" />

This will show message which shows up like this.

![Asp.net Core Cookie policy accept]( “assets/img/post/cookie-accept-asp-net-core.png”relative_url )

and if we accept this then the TempData will work.

Conclusion

TempData uses cookie which is disabled by ASP.NET Core as default. So we can achieve either making it essential or giving message to accept the cookie.

stackoverflow source

This post is licensed under CC BY 4.0 by the author.