Tech News

What is tempData in MVC?

TempData is also a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTTP Request. This mean only from one page to another.
  • when you redirect, “Tempdata” helps to maintain data between those redirects.
  • It internally uses session variables.
  • It requires typecasting for complex data type and check for null values to avoid error.
  • It’s life is very short and lies only till the target view is fully loaded.
  • It is used to store only one time messages like error messages, validation messages. 

public ActionResult Index()
  var model = new Review()
                Body = "Start",
    TempData["ModelName"] = model;
    return RedirectToAction("About");
<pre><pre lang="cs">public ActionResult About()
    var model= TempData["ModelName"];
    return View(model);
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x