{"id":589,"date":"2015-11-06T14:29:00","date_gmt":"2015-11-06T14:29:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=589"},"modified":"2025-02-18T13:48:19","modified_gmt":"2025-02-18T13:48:19","slug":"viewdata-viewbag-tempdata","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/viewdata-viewbag-tempdata","title":{"rendered":"What Are ViewData, ViewBag, &amp; TempData in ASP.NET MVC?"},"content":{"rendered":"\n<p><span lang=\"EN-GB\">I have often seen entry-level developers struggle with the differences between and usage of ViewData, ViewBag, and TempData in ASP.NET MVC. And while there are many articles and blog posts on this topic out there, I\u2019ll try to explain it simply.<\/span><\/p>\n\n\n\n<p><span lang=\"EN-GB\">To start with, ViewData, ViewBag, and TempData all three are objects in ASP.NET MVC that are used to carry or pass data in different scenarios. You may have a requirement to pass data in the following cases:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span lang=\"EN-GB\">Pass data from the controller to view;<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Pass data from one controller to another controller;<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Pass data from one action to another action;<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Pass data between subsequent HTTP requests<\/span><\/li>\n<\/ul>\n\n\n\n<p><span lang=\"EN-GB\">&nbsp;<\/span><\/p>\n\n\n\n<p><span lang=\"EN-GB\">At a higher level, we can depict the use of ViewData, ViewBag, and TempData as shown in the image below: <\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.Maria_5F00_Blogs.November\/3757.DJ_5F00_post_5F00_1_5F00_pic.png\" alt=\"At a higher level, we can depict the use of ViewData, ViewBag, and TempData as shown in the image\" title=\"At a higher level, we can depict the use of ViewData, ViewBag, and TempData as shown in the image\"\/><\/figure>\n\n\n\n<div class=\"\" style=\"line-height: 12pt; margin: 0in 0in 8pt;\"><span lang=\"EN-GB\"><\/span><\/div>\n\n\n\n<div class=\"\" style=\"line-height: 12pt; margin: 0in 0in 8pt;\">&nbsp;<\/div>\n\n\n\n<div class=\"\" style=\"line-height: 12pt; margin: 0in 0in 8pt;\"><span lang=\"EN-GB\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading MsoNormal\" class=\"wp-block-heading MsoNormal\" id=\"passing-data-from-controller-to-view\">Passing Data from Controller to View <\/h2>\n\n\n\n<p><span lang=\"EN-GB\">Let us consider a scenario where you\u2019re passing data from the controller to view. Usually, we pass complex data to the view using the model. Here let\u2019s say we have a strongly typed View which is using the data model List as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public ActionResult Index()\n   {\n       List&lt;Product> p = new List&lt;Product>() {\n \n       new Product { Id = 1, Name = \"Pen\", Price = 300 },\n       new Product { Id = 2, Name = \"Pencil\", Price = 100 }\n   };\n \n   return View(p);\n}<\/pre>\n\n\n\n<p><span lang=\"EN-GB\">On the View, data is displayed by rendering the model as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;table class=\"table\">\n    &lt;tr>\n        &lt;th>\n            @Html.DisplayNameFor(model => model.Name)\n        th>\n        &lt;th>\n            @Html.DisplayNameFor(model => model.Price)\n        th>\n        &lt;th>th>\n    tr>\n \n@foreach (var item in Model) {\n    &lt;tr>\n        &lt;td>\n            @Html.DisplayFor(modelItem => item.Name)\n        td>\n        &lt;td>\n            @Html.DisplayFor(modelItem => item.Price)\n        td>\n        &lt;td>\n            @Html.ActionLink(\"Edit\", \"Edit\", new { id=item.Id }) |\n            @Html.ActionLink(\"Details\", \"Details\", new { id=item.Id }) |\n            @Html.ActionLink(\"Delete\", \"Delete\", new { id=item.Id })\n        td>\n    tr>\n}\n \n&lt;\/table><\/pre>\n\n\n\n<p><span lang=\"EN-GB\">Now we have a requirement to pass data (other than a model) to the view from the controller. There are two possible ways data can be passed. <\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.Maria_5F00_Blogs.November\/4338.DJ_5F00_post_5F00_2_5F00_pic.png\" alt=\" Now we have a requirement to pass data (other than a model) to the view from the controller.\" title=\"Now we have a requirement to pass data (other than a model) to the view from the controller.\"\/><\/figure>\n\n\n\n<p><span lang=\"EN-GB\">Let us assume that we want to pass a simple string to the view besides the Product data model. <\/span><span lang=\"EN-GB\">&nbsp;<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading MsoNormal\" class=\"wp-block-heading MsoNormal\" id=\"passing-data-using-viewbag\">Passing data using ViewBag<\/h2>\n\n\n\n<p><span lang=\"EN-GB\">We can pass data using the ViewBag as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public ActionResult Index()\n{\n    ViewBag.data1 = \"I am ViewBag data\";\n    return View(p);\n}<\/pre>\n\n\n\n<p><span lang=\"EN-GB\">On the view, ViewBag data can be read as the property of the ViewBag as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;h2>@ViewBag.data1&lt;\/h2><\/pre>\n\n\n\n<h2 class=\"wp-block-heading MsoNormal\" class=\"wp-block-heading MsoNormal\" id=\"passing-data-using-viewdata\">Passing data using ViewData<\/h2>\n\n\n\n<p><span lang=\"EN-GB\">We can pass data using the ViewData as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public ActionResult Index()\n{    \n    ViewData[\"data1\"] = \"I am ViewBag data\";\n    return View(p);\n}<\/pre>\n\n\n\n<p><span lang=\"EN-GB\">On the view, ViewData data can be read as the string value pair of the ViewData as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;h2>@ViewData[\"data1\"]h2><\/pre>\n\n\n\n<p><span lang=\"EN-GB\">Let us examine the differences between ViewData and ViewBag. ViewBag is a dynamic property which is based on the dynamic type, whereas ViewData is a dictionary object. We can read data from ViewBag as a property and from ViewData as a key-value pair. Some bullet points about both are as follows:<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading MsoNormal\" class=\"wp-block-heading MsoNormal\" id=\"viewdata\">ViewData<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span lang=\"EN-GB\">It\u2019s a property of type ViewDataDictionary class. <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Data can be passed in the form of a key-value pair.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">To read the complex type data on the view, typecasting is required.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">To avoid the exception, null checking is required. <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Life of ViewData is restricted to the current request and becomes Null on redirection. <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">ViewData is a property of the ControllerBase class<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading MsoNormal\" class=\"wp-block-heading MsoNormal\" id=\"viewbag\">ViewBag<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span lang=\"EN-GB\">It\u2019s a property of the dynamic type.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Data is passed as a property of the object.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">There is no need of typecasting to read the data.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">There is no need for null checking.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Life of ViewBag is restricted to the current request and becomes Null on redirection.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">ViewBag is a property of ControllerBase class.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span lang=\"EN-GB\">In the ControllerBase class, both are defined as property as shown in the image below:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.Maria_5F00_Blogs.November\/3757.DJ_5F00_post_5F00_1_5F00_pic.png\" alt=\" In the ControllerBase class, both are defined as property as shown in the image\" title=\"In the ControllerBase class, both are defined as property as shown in the image\"\/><\/figure>\n\n\n\n<p><span lang=\"EN-GB\">We can summarize ViewBag and ViewData as objects that are used to pass data from the controller to view in a single cycle. The value assigned in ViewBag and ViewData get nullified in the next HTPP request or navigating to another view. <\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"tempdata\">TempData<\/h2>\n\n\n\n<p><span lang=\"EN-GB\">One of the major attributes of both ViewData and ViewBag are that their lifecycle is limited to one HTTP request. On redirection, they lose the data. We may have another scenario to pass data from one HTTP request to the next HTTP request; for example, passing data from one controller to another controller or one action to other action. TempData is used to pass data from one request to the next request. <\/span><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.Maria_5F00_Blogs.November\/7043.DJ_5F00_post_5F00_3_5F00_pic.png\" alt=\" TempData\" title=\"TempData\"\/><\/figure>\n\n\n\n<p><span lang=\"EN-GB\">Let us say that we want to navigate to Read action from Index action and while navigating, pass data to the Read action from the Index action.&nbsp; So in the Index action, we can assign a value to TempData as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> public ActionResult Index()\n {\n    TempData[\"data1\"] = \"I am from different action\";\n    return RedirectToAction(\"Read\");      \n }<\/pre>\n\n\n\n<p><span lang=\"EN-GB\">We can read TempData as a key-value pair. In the Read action, TempData can be read as shown in the listing below:<\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public string Read()\n{\n     string str;\n     str = TempData[\"data1\"].ToString();\n     return str;\n}<\/pre>\n\n\n\n<p><span lang=\"EN-GB\">Like ViewData, TempData is also a dictionary object and to read the data, typecasting and null checking is required. Keep in mind that TempData can persist data only to the subsequent HTTP request. When you are very sure about the redirection, then use TempData to pass the data. <\/span><\/p>\n\n\n\n<p><span lang=\"EN-GB\">Some points about TempData are as follows:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span lang=\"EN-GB\"><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\"> <\/span><\/span><\/span>TempData is used to pass data from one HTTP request to next HTTP request. <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">In other words, TempData is used to pass data from one controller to another controller or action to another action.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">TempData is a property of BaseController class. <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">TempData stores data in a session object <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">TempData is a property of ControllerBase class<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">To read data, &nbsp;Typecasting and null checking are required. <\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">Type of TempData is TempDataDictionary.<\/span><\/li>\n\n\n\n<li><span lang=\"EN-GB\">TempData works with HTTP redirection like HTTP 302\/303 status code<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"summary\">Summary<\/h2>\n\n\n\n<p><span lang=\"EN-GB\">ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used. To pass data from one controller to another controller, TempData can be used. <\/span><\/p>\n\n\n\n<p><span lang=\"EN-GB\">I hope now the concepts of ViewBag, ViewData and TempData are a bit clearer \u2013 and thanks for reading!<\/span><\/p>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p><em>Try our jQuery HTML5 controls for your web apps and take immediate advantage of their stunning capabilities. <a href=\"\/downloads\/request\/00000000-0000-0000-0000-000000005091\">Download Free Trial now!<\/a><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"\/downloads\/request\/00000000-0000-0000-0000-000000005620\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/download.infragistics.com\/marketing\/Blog-in-content-ads\/Ignite-UI-JavaScript\/Blog-incontent-IgniteUI-650x200.jpg\" alt=\" \"\/><\/a><\/figure>\n\n\n\n<div class=\"\" style=\"line-height: 12pt; margin: 0in 0in 8pt;\"><span lang=\"EN-GB\"><\/span><\/div>\n\n\n\n<div class=\"\" style=\"line-height: 12pt; margin: 0in 0in 8pt;\"><span lang=\"EN-GB\">&nbsp;<\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Some developers may struggle with the differences and usage of ViewData, ViewBag, and TempData in ASP.NET MVC. This blog post will explain it all.<\/p>\n","protected":false},"author":65,"featured_media":1126,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,11],"tags":[],"class_list":["post-589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","category-ultimate"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":14,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":1875,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/589\/revisions\/1875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/1126"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}