Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ViewBag, ViewData, or TempData in Mvc3

When to use ViewBag, ViewData, or TempData in view. In the controller i want to send object to the view.I want to know that which will be best in this case. I want the object in the view page.

like image 605
user1006544 Avatar asked Nov 08 '11 06:11

user1006544


People also ask

When should I use ViewBag ViewData or TempData?

To summarize, ViewBag and ViewData are used to pass the data from Controller action to View and TempData is used to pass the data from action to another action or one Controller to another Controller.

When should we use ViewData in MVC?

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. ViewData contains key-value pairs which means each key must be a string in a dictionary. The only limitation of ViewData is, it can transfer data from controller to view.

Is ViewData faster than ViewBag in MVC?

ViewBag will be slower than ViewData; but probably not enough to warrant concern.

When should we use TempData in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.


2 Answers

Use TempData when you need data to be available for the next request, only.

TempData["myInfo"] = "my info";

Then in the next request, it will be there... but will be gone after that.

Use ViewBag for most of your extra-data needs to pass to your view, beyond the @model

ViewBag.MyInfo = "my info";

Then access it from your view.

Use ViewData to access/enter the exact same info as ViewBag, except as a collection instead of properties of a dynamic object.

ViewData["MyInfo"]

accesses exactly the same data as ViewBag.MyInfo

Note that I used strings, but these can store any object you wish.

Another thing to note: TempData and ViewData are both dictionaries that store object values, so you must cast those to their original type when you use them. ViewBag however uses dynamic, and you don't always need to cast that, since it is done at runtime. Some methods (like Extension methods) can not handle dynamic though, so you would need to cast in those cases.

like image 131
Andrew Barber Avatar answered Nov 20 '22 18:11

Andrew Barber


IMHO for decent design practies -

ViewBag = never. ViewData = never. These are magic string-ish based fields and canot be caught during any compile time instances either.

Your VieWModel should contain everything it needs. Thats it's purpose in life. Don't devoid it of its purpose. TempData for status messages only or object you don't want to cache but wan't available for the very next request only.

like image 20
Adam Tuliper Avatar answered Nov 20 '22 17:11

Adam Tuliper