ViewData vs ViewBag vs TempData objects in mvc3 razor
Posted By : Shailendra Chauhan, 19 Jul 2012
Updated On : 16 Jan 2013
Version Support : MVC4 & MVC3
In Asp.net MVC 3 & MVC4, we have three objects - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Now question is that when to use ViewData, VieBag and TempData. All these three objects have its own importance. In this article I am trying to expose the use of these three.
ViewData
- ViewData is a dictionary object that is derived from ViewDataDictionary class.
- ViewData is used to pass data from controller to corresponding view.
- It’s life lies only during the current request.
- If redirection occurs then it’s value becomes null.
- It’s required typecasting for complex data type and check for null values to avoid error.
ViewBag
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
- It’s life also lies only during the current request.
- If redirection occurs then it’s value becomes null.
- It doesn’t required typecasting for complex data type.
TempData
- TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
- TempData is used to pass data from current request to subsequent request means incase of redirection.
- It’s life is very short and lies only till the target view is fully loaded.
- It’s required typecasting for complex data type and check for null values to avoid error.
- It is used to store only one time messages like error messages, validation messages.
ViewData, ViewBag and TempData Current Request Example
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- var emp = new Employee
- {
- EmpID=101,
- Name = "Deepak",
- Salary = 35000,
- Address = "Delhi"
- };
- ViewData["emp"] = emp;
- ViewBag.Employee = emp;
- TempData["emp"] = emp;
- return View(); }
- }
- @model MyProject.Models.EmpModel;
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- ViewBag.Title = "Welcome to Home Page";
- var viewDataEmployee = ViewData["emp"] as Employee; //need typcasting
- var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
- }
- <h2>Welcome to Home Page</h2>
- <div>
- <a href="/Employees">
- <img src='@Url.Content("\\Content\\Images\\101.jpg")' alt="Best Employee "/>
- </a>
- <div>
- This Year Best Employee is!
- <h4>@ViewBag.emp.Name</h4>
- <h3>@viewDataEmployee.Name</h3>
- <h2>@tempDataEmployee.Name</h2>
- </div>
ViewData, ViewBag and TempData Next Request Example
- public ActionResult About()
- {
- var emp = new Employee {
- EmpID=101,
- Name = "Deepak",
- Salary = 35000,
- Address = "Delhi"
- };
- ViewData["emp"] = emp;
- ViewBag.Employee = emp;
- TempData["emp"] = emp;
- //After the redirection, ViewData and ViewBag objects will be null
- //Only TempData will exist after redirection
- return new RedirectResult(@"~\About\");
- }
- @model MyProject.Models.EmpModel;
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- ViewBag.Title = "About";
- var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
- }
TempData with Keep method
If you want to keep value in TempData object after request completion, you need to call Keep method with in the current action. There are two overloaded Keep methods to retains value after current request completion.- void Keep()Calling this method with in the current action ensures that all the items in TempData are not removed at the end of the current request.- @model MyProject.Models.EmpModel;
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- ViewBag.Title = "About";
- var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
- TempData.Keep(); // retains all strings values
- }
 
- void Keep(string key)Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.- @model MyProject.Models.EmpModel;
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- ViewBag.Title = "About";
- var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
- TempData.Keep("emp"); // retains only "emp" string values
- }
 
Key point about TempData and TempData.Keep()
- Items in TempData will only tagged for deletion after they have read.
- Items in TempData can be untagged by calling TempData.Keep(key).
- RedirectResult and RedirectToRouteResult always calls TempData.Keep() to retain items in TempData.
Summary
In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
 
No comments:
Post a Comment