We know that ViewData
or ViewBag
are used to pass information from the controller to the view.
Is there any difference between both? For example, in terms of performance and whether the use of one in particular is recommended.
We know that ViewData
or ViewBag
are used to pass information from the controller to the view.
Is there any difference between both? For example, in terms of performance and whether the use of one in particular is recommended.
Basically, the differences consist of the following:
ViewData
ViewData
is a strongly typed dictionary object into which data is entered via the familiar "key
/value
" syntax.ViewData
derives from the classViewDataDictionary
.ViewData["Dato"] = 1
@ViewData["Dato"]
ViewBag
ViewBag
is a dynamically typed object that allows you to create properties on the fly .ViewBag.Dato = 1
@ViewBag.Dato
In general, the use of one or the other (I think) will depend on the way that suits you best, both work for you to pass information from the controller to the view, unless you want to carry that information during more than one request, so in that case consider using the data dictionary
TempData
in conjunction with itsKeep()
.References to the official documentation:
ViewData
ViewBag
TempData
ViewBag
is just a wrapper overViewData
. The difference is that while itViewData
is of typeViewDataDictionary
(which implements aIDictionary<string, object>
, itViewBag
is of typedynamic
which simplifies the code slightly.Example:
As for the difference in performance it should be negligible, better worry about other places that can really be a bottleneck such as database access
As for which is better, the syntax of
dynamic
is slightly shorter, but it's a matter of taste. Note that neither option is strongly typed which can lead to runtime errors if you mistype the key in this casePersona
. The ideal actually is to pass all the information between the controller and the view through aModel
In reality they are still two sides of the same coin.
The main difference is that while ViewData is a dictionary of objects in which you can access elements of type Object through a string, ViewBag is a dynamic object in which you can access elements through dynamically created properties and that can have a defined data type.
At the moment of truth MVC stores the information in the same container so that a value defined in the controller as
ViewData["dato1"] = xxxxx
can be accessed from the view asViewBag.dato1
and vice versa.They both have the same goal of passing data from the controller to the view.
The ViewData is of type Object and you have to cast in the view to use it, while the ViewBag is generated dynamically with the assigned data type.
ViewData: Requires type casting for data of complex types and checks for null values to avoid errors.
ViewBag: Does not require type casting for complex data types.
Controller:
View:
first of all thank you for your responses. They have clarified my doubt, however I would like to publish another answer in order to help clarify this issue, although I think they have already covered everything quite well.
Technically, all data is passed from controllers to views via a ViewDataDictionary (a specialized dictionary class) called ViewData. You can set and read values in the ViewData dictionary using the standard dictionary syntax as follows:
Although this continues to be available, ASP.NET MVC 3 took advantage of the C# 4 dynamic keyword to allow for simpler syntax. ViewBag is a dynamic container around ViewData. Allows you to set the values as follows:
Thus, ViewBag.CurrentTime is equivalent to ViewData["CurrentTime"]. Generally, most of the actual code you'll come across uses ViewBag instead of ViewData for the most part. You have no real technical advantage in choosing one syntax over the other. ViewBag is just a syntax shortcut that some people prefer over dictionary syntax. It just looks better.
Although you may not have a technical advantage in choosing one format over the other, you should be aware of some important differences between the two. Syntax One obvious difference is that ViewBag works only when the key you are accessing is a C identifier. # valid. For example, if you put a value in the ViewData ["Key With Spaces "], you can't access that value with the ViewBag because the code won't compile. Another key issue to consider is that dynamic values cannot be passed as parameters to extension methods. The C# compiler must know the actual type of each parameter at compile time to choose the correct extension method. If any parameter is dynamic, the build will fail. For example, this code always fails: @Html.TextBox("name", ViewBag. Yam). To avoid this, either Use ViewData["Name"] or cast the value to a specific type: (string) ViewBag.Name.
ViewDataDictionary is a specialized dictionary class, not just a generic Dictionary . One reason for this is that it has an additional model property that allows a specific Model Object to be made available to the view. Because you can only have one model object in the ViewData, using this to pass a specific class to the view is convenient. This allows your view to specify the class that expects the desired model object, which means you can take advantage of strongly typing.
This is translated from a book which I consider one of the best I've read Professional ASP.Net MVC5