Use TempData in ASP.NET Core Razor Pages

On the Visual Studio, create new ASP.NET Core Web Application project

Select Empty Template

Click Ok button to Finish




Open Startup.cs file and add new configurations as below:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace LearnASPNETCoreRazorPagesWithRealApps
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
}

Select Views folder and right click to select Add\New Item Menu

Select Web\ASP.NET in left side. Select Razor View Imports item and click Add button to Finish

In _ViewImports.cshtml file and TagHelpers library as below:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers




Create new folder named Models. In Models folder, create new entities class as below:

Create new class named Product.cs as below:

namespace LearnASPNETCoreRazorPagesWithRealApps.Models
{
    public class Product
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    }
}

Create new folder named Helpers. In this folder, create new helper named TempDataHelper.cs as below:

using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Newtonsoft.Json;

namespace LearnASPNETCoreRazorPagesWithRealApps.Helpers
{
    public static class TempDataHelper
    {
        public static void Put<T>(this ITempDataDictionary tempData, string key, T value) where T : class
        {
            tempData[key] = JsonConvert.SerializeObject(value);
        }

        public static T Get<T>(this ITempDataDictionary tempData, string key) where T : class
        {
            object o;
            tempData.TryGetValue(key, out o);
            return o == null ? null : JsonConvert.DeserializeObject<T>((string)o);
        }
    }
}




Create new folder named Pages. In this folder, create new Razor Page named Index as below:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using LearnASPNETCoreRazorPagesWithRealApps.Models;
using LearnASPNETCoreRazorPagesWithRealApps.Helpers;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class IndexModel : PageModel
    {
        [BindProperty]
        public Product product { get; set; }

        public void OnGet()
        {
            product = new Product();
        }

        public IActionResult OnPost(int age, string username, Product product)
        {
            TempData["age"] = age;
            TempData["username"] = username;
            TempDataHelper.Put(TempData, "product", product);
            return RedirectToPage("ShowFlashAttributes");
        }

    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.IndexModel
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/style.css" rel="stylesheet" type="text/css" />
</head>
<body>

    <form method="post" asp-controller="demo" asp-action="save">
        <fieldset>
            <legend>Single Values</legend>
            <table>
                <tr>
                    <td>Age</td>
                    <td><input type="text" name="age" /></td>
                </tr>
                <tr>
                    <td>Username</td>
                    <td><input type="text" name="username" /></td>
                </tr>
            </table>
        </fieldset>

        <fieldset>
            <legend>Product Information</legend>
            <table>
                <tr>
                    <td>Id</td>
                    <td>
                        <input asp-for="product.Id" />
                    </td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td>
                        <input asp-for="product.Name" />
                    </td>
                </tr>
                <tr>
                    <td>Price</td>
                    <td>
                        <input asp-for="product.Price" />
                    </td>
                </tr>
            </table>
        </fieldset>
        <input type="submit" value="Save" />
    </form>

</body>
</html>




In Pages folder, create new Razor Page named ShowFlashAttributes as below:

using LearnASPNETCoreRazorPagesWithRealApps.Helpers;
using LearnASPNETCoreRazorPagesWithRealApps.Models;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class ShowFlashAttributesModel : PageModel
    {
        public Product product { get; set; }
        public int age { get; set; }
        public string username { get; set; }

        public void OnGet()
        {
            age = int.Parse(TempData["age"].ToString());
            username = TempData["username"].ToString();
            product = TempDataHelper.Get<Product>(TempData, "product") as Product;
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.ShowFlashAttributesModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ShowFlashAttributes</title>
</head>
<body>

    <h3>Single Values</h3>
    <table border="1">
        <tr>
            <td>Age</td>
            <td>@Model.age</td>
        </tr>
        <tr>
            <td>Username</td>
            <td>@Model.username</td>
        </tr>
    </table>

    <h3>Product Info</h3>
    <table border="1">
        <tr>
            <td>Id</td>
            <td>@Model.product.Id</td>
        </tr>
        <tr>
            <td>Name</td>
            <td>@Model.product.Name</td>
        </tr>
        <tr>
            <td>Price</td>
            <td>@Model.product.Price</td>
        </tr>
    </table>

</body>
</html>




Open Index Razor Page with following url: http://localhost:1115

Click Save button submit form to OnPost action in Index razor page and continue redirect to ShowFlashAttributes razor page with following url: http://localhost:1115/ShowFlashAttributes

Output