Why not to use ViewBag heavily?
Asked Answered
K

3

2

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?

Kelleher answered 22/3, 2013 at 10:19 Comment(1)
see also https://mcmap.net/q/400738/-viewmodels-or-viewbagStrom
P
13

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.

Pervade answered 22/3, 2013 at 10:34 Comment(1)
what if use them heavily ?Kelleher
L
5

Two points I know:

  1. It makes your controllers less testable.
  2. Your views are not strongly typed
Lay answered 22/3, 2013 at 10:23 Comment(0)
L
-2

It has the possibility to hit performance at run-time.

Lyndalynde answered 19/6, 2015 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.