In this article I will explain with an example, how to store data in TempData in ASP.Net MVC Razor.
This article will illustrate how to store any value or data in TempData object and later use it inside another Controller or View in ASP.Net MVC Razor.
 
 
Store data in TempData
In the below example, a string value is set in the TempData object in Controller and it is then displayed in View.
Controller
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        TempData["Message"] = "Hello MVC!";
        return View();
    }
}
 
View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @TempData["Message"]
    </div>
</body>
</html>
 
 
Screenshot
Store data in TempData in ASP.Net MVC
 
 
Downloads