Using TempData in ASP.NET CORE 3.0

TempData in MVC is used to pass data from Controller to Controller or Controller to view and it is used to pass data that persists only from one request to the next.


TempData requires Typecasting. And also check for Null value before reading the value from it.

Now let’s see how to assign values to TempData and retrieve it.
For the demo, we are going to add a new Controller with Name Demo Controller in controller folder and add an action method Index in it to handle HTTP get request.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

I have added a controller and action method now let’s assign value to TempData.

Assigning a value to TempData
We have declared TempData with key Message and assigned value “Hello” to it.

using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            TempData["Message"] = "Hello";

            return View();
        }
    }
}

Now let’s add Index view to Read TempData on View.
To add view right, click inside the Index action method of Demo controller then select add view give a name for that view (Index.cshtml).

Retrieve TempData value
After adding view next we are going read TempData value on view.

<h1>Index</h1>

@TempData["Message"]

Various Method in TempData

  • Keep : – Marks all keys in the dictionary for retention.
  • Keep (with Key): – Marks the specified key in the dictionary for retention.
  • Peek (with Key): – Read data from key without marking the key for deletion.

Now let’s see demo how to use Keep Method.
We have already have added demo controller and it also has a view, next we are going to add a new controller with name demo2 with index action method in it.
First, we are going to create a TempData with key “Process_Message” inside Demo Controller Index action method and assign value to it.

using System;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            TempData["Process_Message"] = "Process Completed";

            return View();
        }
    }
}

After assigning value next on Index View of DemoController we are going to read the value of TempData with key “Process_Message

<h1>Index</h1>
Message :- @TempData["Process_Message"]
<br />
Link :-
<a href="/Demo2/Index">Redirect to Demo2 Controller </a>

After clicking on the link, we are redirected to Index action method of demo2 controller where we are again reading data from TempData with key “Process_Message” but data is null now.

Now to keep all data of Temp Data keys we can use TempData.keep.

<h1>Index</h1>

Message :- @TempData["Process_Message"]
@{
    TempData.Keep();
}
<br />
Link :-
<a href="/Demo2/Index">Redirect to Demo2 Controller </a>

Now if you again read data after adding TempData.Keep(); then data will be available.

Next thing we are going to see an example of Keep (with Key).

Keep (with Key)
Marks the specified key in the dictionary for retention
If you want to retain only data of single key then you can use Keep Method with the key.
We are going to use the same controller which we already have used for TempData.Keep() next step
we are going to add 2 Temp Data in Demo Controller with key “Process_Message” and “Message“.

using System;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class Demo2Controller : Controller
    {
        public IActionResult Index()
        {
            TempData["Process_Message"] = "Process Message";

            TempData["Message"] = "Message";

            return View();
        }
    }
}

Next on View we are going to Use Keep (with Key) for Key “Process_Message” and we are not going to Use keep for “Message” key and along with TempData, we are going to add hyperlink to navigate to Demo2 Controller Index action method where we are going to read values from TempData again to see which TempData value get NULL.

<h1>Index</h1>

Process_Message :- @TempData["Process_Message"]
@{
    TempData.Keep("Process_Message");
}
<br />

Message :- @TempData["Message"]
<br />
Link :-
<a href="/Demo2/Index">Redirect to Demo2 Controller </a>

As you see here, we are able to see message from both TempData but after we click on Hyperlink to navigate then only “Process_Message” key data will be seen because have use Keep data with key which Marks the specified key in the dictionary for retention and we have not used any method for “Message” key retention.

Peek
Read a value from TempData without marking it for deletion.
If you want to read the value but without marking it for deletion then you can use Peek Method.
We are going to add 2 Temp Data in Demo Controller with key “Process_Message” and “Message” and assign values to it.

using System;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            TempData["Process_Message"] = "Process Message Peek";

            TempData["Message"] = "Message Peek";

            return View();
        }

    }
}

Next on View we are going to Use Peek (with Key) for Key “Process_Message” and we are not going to Use keep for “Message” key and along with TempData, we are going to add a hyperlink to navigate to Demo2 Controller Index action method where we are going to read values from TempData again to see which TempData value get NULL.

<h1>Index</h1>

Process_Message :- @TempData.Peek("Process_Message");

<br />

Message :- @TempData["Message"]
<br />
Link :-
<a href="/Demo2/Index">Redirect to Demo2 Controller </a>

As you see here, we are able to see a message from both TempData but after we click on Hyperlink to navigate then only “Process_Message” key data will be seen because have use Peek data with key which Read data from key without marking the key for deletion and we have not used any method for “Message” key retention.

By