In this article I will explain with an example, how to use Keep and Peek functions of TempData in ASP.Net MVC Razor.
The Keep function is used to preserve the data of TempData object even after the value is read while the Peek function is used to read the value without clearing it.
 
 
TempData Default functioning
When value is read from the TempData object, the value is cleared and NULL value is assigned.
Controller
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        TempData["Message"] = "Hello";
        return View();
    }
 
    [HttpPost]
    public ActionResult Submit()
    {
        var message = TempData["Message"];
        return View();
    }
}
 
View
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @TempData["Message"]
 
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        <input type="submit" value="Submit"/>
    }
</body>
</html>
 
Screenshot
TempData object value was read in View and hence it is NULL.
ASP.Net MVC: Using TempData Keep and Peek functions
 
 
TempData Keep function
The Keep function is used to preserve the data of TempData object even after the value is read.
Controller
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        TempData["Message"] = "Hello";
        return View();
    }
 
    [HttpPost]
    public ActionResult Submit()
    {
        var message = TempData["Message"];
        return View();
    }
}
 
View
After the value is read, the Keep function is called which will preserve the value in TempData object.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @TempData["Message"]
    @{
       TempData.Keep("Message");
    }
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        <input type="submit" value="Submit"/>
    }
</body>
</html>
 
Screenshot
The value is available in the TempData object even after the value is read.
ASP.Net MVC: Using TempData Keep and Peek functions
 
 
TempData Peek function
The Peek function is used to read the data of TempData object without clearing it.
Controller
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        TempData["Message"] = "Hello";
        return View();
    }
 
    [HttpPost]
    public ActionResult Submit()
    {
        var message = TempData["Message"];
        return View();
    }
}
 
View
After the value is read, the Keep function is called which will preserve the value in TempData object.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @TempData.Peek("Message")
 
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        <input type="submit" value="Submit"/>
    }
</body>
</html>
 
Screenshot
The value is available in the TempData object even after the value is read.
ASP.Net MVC: Using TempData Keep and Peek functions