Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not to use ViewBag heavily?

What if i use too many ViewBag in MVC3 page? What extreme problems will be there if i continue use them heavily. In less words, what are the disadvantages of ViewBag and what is the alternate for it?

like image 662
A.T. Avatar asked Jan 17 '26 07:01

A.T.


2 Answers

what is the alternate for it?

You should only use ViewModels, It's the recommended approach. Create Strongly typed views. It would make your code cleaner and easy to maintain.

What extreme problems will be there?

Issue with Viewbags/ViewData is compile time checking. ViewBags are just dictionaries and you won't be aware about any issues until run time and thus it makes your code error-prone. So always try to go with ViewModels.

like image 106
ssilas777 Avatar answered Jan 20 '26 21:01

ssilas777


Two points I know:

  1. It makes your controllers less testable.
  2. Your views are not strongly typed