ViewData in ASP.NET Core

ViewData :- ViewData is a Dynamic Property of ControllerBase Class that is used for sending data from Contoller to View. It is a Dictionary object having key value pair. Key is the name of ViewData and value is the stored data into it. Controller may have so many Action methods but ViewBag can only send data to views of respective action methods where the ViewData property is used.

ViewData do not have a Compile Time error checking mechanism because it uses the dynamic types like var in C# that decided the type at run time and accroding to data it will work. ViewData is required typecasting for different types of data stored into it. There is no any typecasting required for String data other than string it is required the typecasting.

Note :- ViewData can send data from Controller to View only and it is not possible to send data from View to Controller using the ViewData. After the Redirection value of ViewData always be null it is valid only for single HTTP request.

How to pass and retrieve data from ViewData in ASP.Net Core

Image is not available

 public class TestController : Controller
 {   private readonly AppDbContext db;
     public TestController(AppDbContext _db)
     {
       db = _db;
     }
     public IActionResult ViewDataExample()
     {
       var dropdownItems = db.tblCountries.ToList();
       ViewData["Topictitle"] = "ViewData Example";
       iewData["TotalRecordsCount"] = dropdownItems.Count;
       ViewData["Country"] = new SelectList
       (dropdownItems, "CountryId", "CountryName");
       return View();
     }
 } 

I have attached a screen image above in which you can see , I have created a Test Controller and inside the Test Controller I created a ViewDataExample action method. In the ViewDataExample method I have fetched country records from tblCountries table in SQL Database and assigned it to a variable. After that I used a Country dynamic ViewData key to store these data. Apart from it I have created two different ViewData keys that is Topictitle used for storing the topic title and second one is TotalRecordsCount for storing the number of records in dropdownlist. Now create a View for ViewDataExample method as shown in below image.

Image is not available

I have attached a screen image above in which you can see , I have retrieved the ViewData data and used into heading tag. For retrieving the ViewData values I have used ViewData["Topictitle"], ViewData["TotalRecordsCount"] and "var country = ViewData["Country"] as SelectList;" with the typecasting of SelectList types. In ViewData there is a need of Typecasting for data based on whether it holds string data or any other complex types like a list or object. Run the application and you can see the final output as like as in below image.

Image is not available

When should to use ViewData

  • When there is a requirement to send small amount of data from controller to view then you can use it.
  • When there is a requirement to display error messages or notifications on view then you can use it.
  • When there is a requirement to send dynamically created data and that is not handling using the model class and typecasting is required then you can send it by ViewData.
  • When data is loosely coupled with view models.
  • When do not contain critical information because on errors it may be harmful.

Advantages of ViewData in ASP.Net Core

  • Due to dynamic proprty of ViewData you no need to define a Model for small amount of data so that it reduces the coding efforts.
  • Easy to implement and easy to retrieve the data from ViewData using key and values.
  • We can store any kind of data into it so that it provides flexible use in many cases.
  • Good enough to use in the applications where no need to define much models.

Disadvantages of ViewData in ASP.Net Core

  • It is limited to current http request only redirection leads null values.
  • There is no mechanism to checking the error on compile time so it leads to run time errors and difficult to debug.
  • No support to intellisense so that speed of development is very slow and possiblities of error will increase due to lack of intellisense.
  • Due to run time binding with dynamic proerty it leads some overhead on performnace and slow down the application.
  • It is not suitable for complex data structures that need more manipulation and opeartions.
  • Fully risky for Nullrefrence exceptions that has more probabilities to occur.


Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.