Which is faster Viewdata or Viewbag
Before going to see which is faster viewdata or viewbag? Let's discuss what is viewbag and viewdata and when to use viewbag and viewdata?. The purpose of using viewbag and viewdata is the same to transfer data from controller to view. Viewbag is the dynamic property and viewdata is the key-value pair dictionary. Let's see in detail both viewbag and viewdata.
ViewBag:
Viewbag is a dynamic feature introduced into C# 4.0. You can be added an object to have properties dynamically added to it. If you have confusion what is mean by dynamic? Then go to viewbag and click F12 then expand viewbag 👇.
[Dynamic]
public dynamic ViewBag { get; }
You will see the below code. For viewbag there is no need to typecast.Basic syntax of viewbag is
👉ViewBag.Data = "This is view bag transfer data";
How to pass data from controller to view with an example using ViewBag
Step 1: Create a simple MVC project in the visual studio.
Step 2: For adding employee controller. Right-click on controller folder -> add controller.
Step 3: Add viewbag code to the action method.
Step 4: Right-click on the Action method. Click on Add View. Uncheck default layout. It is the empty view.
Step 5: Add the code to view for viewing to user transferred data. This is a very basic example for use of Viewbag.
ViewData:
Viewdata also used to transfer data from controller to view. It is a key-value pair dictionary. It needs
to typecast. ViewData is a dictionary of objects that are stored and retrieved using string as a key. The basic structure of viewdata is 👇 .
ViewData["data"] = "This is viewdata";
How to pass data from controller to view with an example ViewData
Step 1: I continued the above project which used in viewbag article. Create a controller for viewdata.
Step 2: Write code in the Action method. Please refer to below for your information.
Step 3:Right-click on the action method and create a view.
Step 4:Add view code.
Another parameter used in MVC to transfer the data is TempData.
When to use ViewBag and ViewData?
Viewbag and viewdata both are used for the same purpose. But viewdata needs to typecast so many developers choose to use viewbag.
Difference between ViewBag and ViewData
ViewData | ViewBag |
---|---|
It is a key-value dictionary | It is a type of object |
ViewData is faster than viewbag | ViewBag is slower than ViewData |
It is only for the current request |
It is also only for the current request |
Conversion of code typecasting is required when enumerating | There is no need for typecasting while enumerating |
Is ViewBag is secure?
Yes, No doubt for viewbag is secure.
1 comments:
Click here for commentsThen I would sit and watch someone sit proper behind me and play the same sport and hit so many bonuses RIGHT AFTER I FINISHED PLAYING! So, earlier than the "profitable pot" had been exhausted, I returned to the original slot sport to try again after a day or two. Hi, really have} just been studying an article by Jungle Jim on slot machines that was posted on your web site, which was very illuminating, and everything he says seems fairly correct. Although the article is from quantity of} years ago, I found the contents gave fairly a representative appraisal of on-line and land-based slot 카지노사이트 machines. I've performed them long sufficient to consider they're not seem to be} random, however as I said, it is my opinion from my observations, and also you're entitled to yours.
ConversionConversion EmoticonEmoticon