Technical Articles

What is tempData in MVC?

0 0
TempData:
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",
                Rating=5
            };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}
<pre><pre lang="cs">public ActionResult About()
{
    var model= TempData["ModelName"];
    return View(model);
}
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
× How can I help you?