{"id":92473,"date":"2021-09-23T14:19:24","date_gmt":"2021-09-23T14:19:24","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=92473"},"modified":"2026-05-25T15:37:12","modified_gmt":"2026-05-25T15:37:12","slug":"what-is-viewdata-and-implement-viewdata-in-asp-net-mvc","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/other\/what-is-viewdata-and-implement-viewdata-in-asp-net-mvc\/","title":{"rendered":"ViewData vs ViewBag vs TempData in ASP.NET MVC: Differences, When to Use Each, and Implementation Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><b>ASP.NET MVC provides three mechanisms for passing data from a controller to a view: ViewData, ViewBag, and TempData. Understanding when to use each is a core MVC skill. This article walks through each mechanism with code examples and a side-by-side comparison.<\/b><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ViewBag, ViewData,&nbsp;and TempData all are Dictionary objects in ASP.NET MVC and these are used for data passing in various situations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following are the <a href=\"https:\/\/www.ifourtechnolab.com\/blog\/what-is-tempdata-and-how-to-use-in-mvc\">situations where we can use TempData<\/a> and other objects.<\/p>\n\n\n<div class=\"block-core-list\">\n<ol class=\"wp-block-list\">\n<li>Pass the data from Controller to View.<br><br><\/li>\n\n\n\n<li>Pass the data from an action to another action in Controller.<br><br><\/li>\n\n\n\n<li>Pass the data in between Controllers.<br><br><\/li>\n\n\n\n<li>Pass the data between consecutive requests.<\/li>\n<\/ol>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-dictionary-object\">What is Dictionary Object?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In C#, Dictionary is used to store the key-value pairs. The Dictionary is the same as the non-generic hashtable. It can be defined under System.Collection.Generic namespace. It has a dynamic nature which means the size of the dictionary grows according to the need.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System;\nusing System.Collections.Generic;  \n  \nclass Demo {\n  \n    static public void Main () {\n          \n        Dictionary&lt;int, string&gt; My_dict1 =  \n                       new Dictionary&lt;int, string&gt;(); \n            \n          My_dict1.Add(1123, \"Welcome\");\n          My_dict1.Add(1124, \"to\");\n          My_dict1.Add(1125, \"Programming\");\n            \n          foreach(KeyValuePair&lt;int, string&gt; ele1 in My_dict1)\n          {\n              Console.WriteLine(\"{0} and {1}\",\n                        ele1.Key, ele1.Value);\n          }\n          Console.WriteLine();\n            \n      Dictionary&lt;string, string&gt; My_dict2 =  \n              new Dictionary&lt;string, string&gt;(){\n                                  {\"a.1\", \"Dog\"},\n                                  {\"a.2\", \"Cat\"},\n                                {\"a.3\", \"Pig\"} }; \n           \n          foreach(KeyValuePair&lt;string, string&gt; ele2 in My_dict2)\n          {\n              Console.WriteLine(\"{0} and {1}\", ele2.Key, ele2.Value);\n          }\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-viewdata\">What is ViewData?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In MVC, when we want to transfer the data from the controller to view, we use ViewData. It is a dictionary type that stores the data internally.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ViewData contains key-value pairs which means each key must be a string in a dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The only limitation of ViewData is, it can transfer data from controller to view. It can not transfer in any other way and it is valid only during the current request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> public ViewDataDictionary ViewData { get; set; }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When we want to use the key-value pair to ViewData,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ViewData[\"DemoText\u201d]=\"This is syntax.\";<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can also use ViewData in the razor view from controller. Here is the syntax for that:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;h1&gt;@ViewData[\"PageTtitle\"]&lt;\/h1&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When we want to add custom objects, array, list, etc, in ViewData, and cast them back in the View. We can use the code snippet as below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public ActionResult actionViewData()\n{\nArrayList alCit\ny = new ArrayList();\nalCity.Add(\"Ahmedabad\");\nalCity.Add(\"Rajkot\");\nalCity.Add(\"Amreli\");\nalCity.Add(\"Bhavnagar\");\nalCity.Add(\"Surat\");\nalCity.Add(\"Junagadh\");\nalCity.Add(\"Vadodara\");\nViewData[\"City\"] = alCity;\nreturn View();\n}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">@{\nArrayList _city = ViewData[\"City\"] as ArrayList;\n    foreach (string city in _city)\n    {\n        &lt;div&gt;@city&lt;\/div&gt;\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"460\" height=\"106\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/09\/word-image-1.png\" alt=\"\" class=\"wp-image-92474\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Figure 1: ViewData Flow<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of ViewData which shows how to transfer data from controller to view using ViewData.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public ActionResult Index()\n{\t\n    IList&lt;Employee&gt; employeeList = new List&lt;Employee&gt;();\n    employeeList.Add(new Employee(){ EmployeeName = \"Hemanshu\" });\n    employeeList.Add(new Employee(){ EmployeeName = \"Harsh\" });\n    employeeList.Add(new Employee(){ EmployeeName = \"Hiren\" });\n    ViewData[\"employees\"] = employeeList;\n  \n    return View();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, ViewData[\u201cemployees\u201d] is assigned to an employeeList where \u201cemployees\u201d is a key and employeeList is a value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To access the ViewData[\u201cemployees\u201d] in the view, here is the snippet of code you can use.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;ul&gt;\n@foreach (var std in ViewData[\"employees\"] as IList&lt;Employee&gt;)\n{\n    &lt;li&gt;\n        @emp.EmployeeName\n    &lt;\/li&gt;\n}\n&lt;\/ul&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In simple terms, these are the ways to store and retrieve data to and for respectively ViewData as shown:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Storing :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ViewData[\u201cemployees\u201d] = employeeList;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Retriving :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">string employee = ViewData[\u201cEmployee\u201d].ToString();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In MVC, ViewData does not check compile-time errors. If we misspell the key names, we would not get any errors but we can identify them at the run time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Controller:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Collections.Generic;\nusing System.Web.Mvc;\nnamespace DemoMvcApplication.Controllers{\n   public class HomeController : Controller{\n      public ViewResult Index(){\n         ViewData[\"Countries\"] = new List&lt;string&gt;{\n            \"India\",\n            \"Malaysia\",\n            \"Dubai\",\n            \"USA\",\n            \"UK\"\n         };\n         return View();\n      }\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">View<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@{\n   ViewBag.Title = \"Countries List\";\n}\n&lt;h2&gt;Countries List&lt;\/h2&gt;\n&lt;ul&gt;\n@foreach(string country in (List&lt;string&gt;)ViewData[\"Countries\"]){\n   &lt;li&gt;@country&lt;\/li&gt;\n}\n&lt;\/ul&gt;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"413\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/09\/word-image.jpeg\" alt=\"\" class=\"wp-image-92475\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Figure 2: Output<\/p>\n\n\n\n<section id=\"my-first-block-block_873c43423c2d3b522d08eaa13d155fbe\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Simple Talk is brought to you by Redgate Software<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Take control of your databases with the trusted Database DevOps solutions provider. Automate with confidence, scale securely, and unlock growth through AI.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/solutions\/overview\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Discover how Redgate can help you: Simple Talk is brought to you by Redgate Software\">Discover how Redgate can help you<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-viewbag\">What is ViewBag?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ViewBag is an object which is dynamically passing the data from Controller to View and this will pass the data as the property of object ViewBag. We do not need to typecast to read the data for null checking here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Controller:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Public ActionResult Index()  \n{  \n    ViewBag.Title = \u201cHello\u201d;  \n    return View();  \n}  <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">View:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;h2&gt;@ViewBag.Title&lt;\/h2&gt;&amp;nbsp;&amp;nbsp;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Controller:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System;  \nusing System.Collections.Generic;  \nusing System.Web.Mvc;  \nnamespace ViewBagExample.Controllers  \n{  \n    public class ViewBagController : Controller  \n    {  \n        public ActionResult Index()  \n        {  \n            List&lt;string&gt; Courses = new List&lt;string&gt;();  \n            Courses.Add(\"J2SE\");  \n            Courses.Add(\"J2EE\");  \n            Courses.Add(\"Spring\");  \n            Courses.Add(\"Hibernates\");  \n            ViewBag.Courses = Courses;  \n            return View();  \n        }  \n    }  \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">View:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;  \n&lt;html&gt;  \n&lt;head&gt;  \n    &lt;meta name=\"viewport\" content=\"width=device-width\" \/&gt;  \n    &lt;title&gt;Index&lt;\/title&gt;  \n&lt;\/head&gt;  \n&lt;body&gt;  \n    &lt;h2&gt;List of Courses&lt;\/h2&gt;  \n    &lt;ul&gt;  \n        @{  \n            foreach (var Courses in ViewBag.Courses)  \n            {  \n                &lt;li&gt; @Courses&lt;\/li&gt;  \n            }  \n        }  \n    &lt;\/ul&gt;  \n&lt;\/body&gt;  \n&lt;\/html&gt;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"253\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/09\/asp-view-bag-2.png\" alt=\"ASP View bag 2\" class=\"wp-image-92476\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Figure 3: Output<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-tempdata\">What is TempData?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">TempData is a dictionary object derived from TempDataDictionary which contains key-pair values. It is useful when we want to transfer the data from the Controller to the View in ASP.NET MVC Application. It stays for a subsequent HTTP request as opposed to other options we discussed prior who stay only for the current request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although it removes the key-value pair once it is accessed, we can keep it using<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Controller:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HomeController : Controller\n{\n    public ActionResult Index()\n    {\n        TempData[\"name\"] = \"Ishan\";\n        return View();\n    }\n    public ActionResult About()\n    {\n        string name;\n        \n        if(TempData.ContainsKey(\"name\"))\n            name = TempData[\"name\"].ToString();\n        return View();\n    }\n    public ActionResult Contact()\n    {\n        return View();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">View:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HomeController : Controller\n{\n    public ActionResult Index()\n    {\n        TempData[\"name\"] = \"Ishan\";\n        return View();\n    }\n    public ActionResult About()\n    {\n        return View();\n    }\n    public ActionResult Contact()\n    {\n        return View();\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-differences-between-viewdata-viewbag-and-tempdata\">Differences between ViewData, ViewBag and TempData:<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\n<p>ViewData<\/p>\n<\/td><td>\n<p>ViewBag<\/p>\n<\/td><td>\n<p>TempData<\/p>\n<\/td><\/tr><tr><td>\n<p>Key-Value Dictionary Object<\/p>\n<\/td><td>\n<p>Type Object<\/p>\n<\/td><td>\n<p>Key-Value Dictionary Collection<\/p>\n<\/td><\/tr><tr><td>\n<p>A property of ControllerBase class<\/p>\n<\/td><td>\n<p>A Dynamic property of ControllerBase class<\/p>\n<\/td><td>\n<p>A property of the controllerBase class<\/p>\n<\/td><\/tr><tr><td>\n<p>Faster<\/p>\n<\/td><td>\n<p>Slower<\/p>\n<\/td><td>\n<p>&#8211;<\/p>\n<\/td><\/tr><tr><td>\n<p>Introduced in MVC 1.0, Available in MVC 1.0 and above<\/p>\n<\/td><td>\n<p>Introduced in MVC 3.0,<\/p>\n<p>Available in MVC 3.0 and above<\/p>\n<\/td><td>\n<p>Introduced in MVC 1.0,<\/p>\n<p>Available in MVC 1.0 and above.<\/p>\n<\/td><\/tr><tr><td>\n<p>Works with .NET Framework 3.5 and above<\/p>\n<\/td><td>\n<p>Works with .NET framework 4.0 and above<\/p>\n<\/td><td>\n<p>Works with .NET framework 3.5 and above<\/p>\n<\/td><\/tr><tr><td>\n<p>Type Conversion code is required<\/p>\n<\/td><td>\n<p>Type Conversion code is not required<\/p>\n<\/td><td>\n<p>Type Conversion code is required<\/p>\n<\/td><\/tr><tr><td>\n<p>Value becomes null if a redirection has occurred<\/p>\n<\/td><td>\n<p>Value becomes null if a redirection has occurred<\/p>\n<\/td><td>\n<p>TempData is used to pass data between two consecutive requests<\/p>\n<\/td><\/tr><tr><td>\n<p>Lies only during the current request<\/p>\n<\/td><td>\n<p>Lies only during the current request<\/p>\n<\/td><td>\n<p>Only works during the current and subsequent request<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"603\" height=\"304\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2021\/09\/word-image-2.png\" alt=\"\" class=\"wp-image-92477\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Figure 4: Summary<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To conclude, it is clear that ViewData is used to pass the data from Controller action to View. Here, we discussed ViewData properties and how to use that in any MVC application.<\/p>\n\n\n\n<section id=\"my-first-block-block_2df37dc27f0f7a756740cba8205539b5\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Stop proving compliance by hand<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Redgate Monitor Enterprise checks your SQL Server configuration against the Center for Internet Security (CIS) Benchmarks\u00ae, using server- and database-level templates.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/monitor.red-gate.com\/GlobalDashboard\" class=\"btn btn--secondary btn--lg\" aria-label=\"Try the demo: Stop proving compliance by hand\">Try the demo<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\n\n\n\n<section id=\"faq\" class=\"faq-block my-5xl\">\n    <h2>FAQs<\/h2>\n\n            <h3 class=\"mt-4xl\">1. What is the difference between ViewData and ViewBag in ASP.NET MVC?<\/h3>\n        <div class=\"faq-answer\">\n            <p>ViewData and ViewBag are the same underlying storage &#8211; a ViewDataDictionary keyed by string, valued by object &#8211; with two different access syntaxes. ViewData uses dictionary syntax: ViewData[&#8216;UserName&#8217;] = &#8216;Alice&#8217; in the controller, @ViewData[&#8216;UserName&#8217;] in the view, requiring a cast if you need typed access like (string)ViewData[&#8216;UserName&#8217;]. ViewBag uses dynamic-property syntax (via C#&#8217;s dynamic type): ViewBag.UserName = &#8216;Alice&#8217; in the controller, @ViewBag.UserName in the view, no cast needed. They&#8217;re interchangeable &#8211; ViewData[&#8216;X&#8217;] and ViewBag.X access the same value. Choose based on preference: ViewBag&#8217;s dynamic syntax is cleaner for reads but provides no compile-time checking (a typo in @ViewBag.UsreName fails silently as null rather than a compile error). ViewData&#8217;s dictionary access is more explicit but more verbose. Both have the same lifetime (current request only) and the same fundamental limitations &#8211; modern ASP.NET MVC best practice is strongly-typed view models for primary page data; ViewData\/ViewBag for ancillary data only.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">2. What is TempData and when should I use it?<\/h3>\n        <div class=\"faq-answer\">\n            <p>TempData is a dictionary-like object (TempDataDictionary) designed for scenarios where data must survive a redirect. Typical example: a controller action processes a form submission and succeeds, then redirects the browser to a confirmation page &#8211; the confirmation message needs to be displayed on the confirmation page, but after the redirect the original controller action&#8217;s ViewData and ViewBag are gone (different request). TempData stores values for exactly one additional request cycle: TempData[&#8216;Message&#8217;] = &#8216;Saved successfully&#8217;; return RedirectToAction(&#8216;Index&#8217;);. The redirected-to action can read @TempData[&#8216;Message&#8217;]; after that read, the value is marked for removal. Backed by session state by default (or cookies in newer configurations), so TempData adds session dependency. Use TempData for: post-redirect flash messages (success confirmations, error notices), short-lived data crossing a redirect boundary. Don&#8217;t use TempData for: data that should persist longer than one request cycle (use session directly), primary page data (use view models), data that doesn&#8217;t cross a redirect (use ViewData\/ViewBag).<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">3. Should I use ViewData or a view model in ASP.NET MVC?<\/h3>\n        <div class=\"faq-answer\">\n            <p>Modern ASP.NET MVC best practice: view models for primary page data, ViewData\/ViewBag for ancillary supporting data only. Rationale: strongly-typed view models provide compile-time checking (typos in property names are errors, not runtime nulls), IntelliSense in the view template, easier refactoring (rename a property, see all usages update), and clearer contracts between controller and view. Pattern: define a view model class (class UserProfileViewModel { public string UserName {get; set;} public int Age {get; set;} public List&lt;Order&gt; RecentOrders {get; set;} }), populate it in the controller, pass to the view via return View(model), and access strongly-typed properties in the view (@Model.UserName). Use ViewData\/ViewBag sparingly for: page title, breadcrumbs, layout-only data that&#8217;s not part of the view&#8217;s primary responsibility, flash messages from TempData. The general rule: if the view&#8217;s template repeatedly references the same data, that data belongs in a view model; if the data is cross-cutting layout concerns, ViewData\/ViewBag is appropriate. Razor Pages (newer) has stronger guidance toward PageModel (its equivalent of view model) as the primary pattern; ViewData\/ViewBag usage continues to decline in modern ASP.NET Core applications.<\/p>\n        <\/div>\n    <\/section>\n","protected":false},"excerpt":{"rendered":"<p>Comparing ViewData, ViewBag, and TempData in ASP.NET MVC &#8211; ViewData as a dictionary object for controller-to-view data, ViewBag as the dynamic-property wrapper around ViewData, and TempData for data that survives a redirect. With code examples and a differences comparison.&hellip;<\/p>\n","protected":false},"author":337514,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[143610],"class_list":["post-92473","post","type-post","status-publish","format-standard","hentry","category-other"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/92473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/337514"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=92473"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/92473\/revisions"}],"predecessor-version":[{"id":110973,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/92473\/revisions\/110973"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=92473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=92473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=92473"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=92473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}