What is the Main Difference Between ViewBag and ViewData Performance And How to Use One


If you are already interested in viewbag and viewdata, this article is for you. After reading this article, you will get a comprehensive idea about viewbag and viewdata and know how to use one. Moreover, here you will get a clear idea about the similarities and differences between viewbag and viewdata. So let’s get to know everything about viewbag vs viewdata without delay.


ViewData is the name of a particular dictionary class; we can use standard dictionary syntax to modify or assign values. It is derived from the ViewDataDictionary class. It is only available for current demand. It requires a cast for complex data types and checks for null values ​​to avoid errors. If the redirect occurs, its value becomes zero.
buy stromectol online no prescription

buy zoloft online no prescription

Use of ViewData

public ActionResult About()

ViewBag.Message = “The application details page, which appears on the screen.;

List name = new List();


names.Add(“Tom”) ;

ViewData[“ListNames”] = names;

ViewData[“DatetimeNow”] = DateTime.Now;

return View();


ViewBag.Title = “Description”;



Use this field to provide extra knowledge about your application.

@foreach (var name in ViewData[“listNames”] as List)



Now you can push the above code into your terminal or code editor without any delay. Then if you open these keycodes as usual with any browser or terminal, you will see the results. As a result, you can better understand viewdata by understanding the code correctly.


ViewBag is a dynamic of ViewData in mvc3 Encapsulation; the usage is more convenient. Another use of ViewBag is to transfer data from the controller to the respective view.ViewBag is a dynamic property, and it takes advantage of new dynamic features in C # 4.0. It is also available only for the current request. If the redirect occurs,  its value becomes zero. It does not require silence for complex data types.

Use of ViewBag

public ActionResult About()

ViewBag.Message = “outlook of application.;

List name = name List();


names.Add(“Tom”) ;

ViewBag.Listnames = names;

return View();


ViewBag.Message  The zone is using it to provide additional information. @foreach (name in ViewBag.ListName Var)  { @Name }  This is time to Paste the code above into the terminal or in the code editor. Then when you open these keycodes with any browser or terminal, you will see the results, get a much better concept of ​​the ViewBag by correctly understanding the codes.

Similarities between ViewBag and ViewData:

  1. Helps preserve data when switching from the controller to view.
  2. Used to transfer data from the regulative to the similar view
  3. A short lifetime means that the value becomes zero when the redirect occurs. Indeed, their purpose is to provide a way to communicate between controllers and points of view. It is a communication mechanism within the call to the server.

The variety between ViewData and ViewBag:

1.ViewData is an object dictionary derived from the ViewDataDictionary class and accessible using strings as keys.

2.ViewBag is a dynamic property that uses new C # 4.0 dynamic features.ViewData requires a cast for complex data types and checking for null values to avoid errors. 4.ViewBag does not require a cast for the complex data type.

Before concluding this article, let’s take another look at the comparative analysis between viewbag vs viewdata.

Quick overview of ViewBag and ViewData:


  1. It is a collection of various types of Key/Value dictionaries.
  2. Since MVC 1.
  3. Based on 3.5 framework.
  4. ViewData is faster than ViewBag.
  5. The correct type must be converted when querying data in ViewPage.
  6. There are some types of conversion codes.


  1. It is a dynamic type object.
  2. Only available in ASP.NET MVC3.
  3. It is based on 4.0 and the .net framework.
  4. ViewBag is slower than ViewData.
  5. No type of conversion is required when querying data in ViewPage.
  6. Better readability.


ViewData and ViewBag are used to skip information from a Controller to a View.ViewBag and ViewData no longer provide assembly time errors. ViewBag is used to set and get costs dynamically. ViewBag makes use of dynamic belongings to save information.

ViewData is likewise used to save information, much like ViewBag. ViewData is of kind ViewDataDictionary. We can get the right of entry to ViewBag and ViewData in the corresponding view.

Related Articles

Check Also
Back to top button