1

I see references to "best practice", but why is it better to use a ViewBag object versus an Application object when accessing static data in the view?

Both are possible. Suppose you want to add an object to every view but not necessarily pass it to view the model (since there may not even be a model for a given view)?

This could be done several ways, like with a global filter, but accessing an application variable is much more convenient.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Pancho
  • 77
  • 4

3 Answers3

1

Both are nasty workarounds that bypass everything that MVC stands for.

In the Model-View-Controller pattern, your Controller prepares a Model containing all values necessary to populate the View, and the View in turn displays the Model properties the way it's intended to.

Any way you use to circumvent that pattern, like ViewBag, Session or Application variables, are anti-patterns that defeat the MVC way - as long as you access those variables directly from the View.

See also Pass data to layout that are common to all pages and Create ViewModel for Navigation: if you need certain values on all pages, consider using a base viewmodel or partial views.

And yes, this discussion borders on pragmatic/purist. If you for example have a menu that's the same for every user, but which is loaded from a database, then sure, go ahead and store it in an Application variable for caching purposes to load it once per AppDomain. But then still use a base ViewModel or Partial to render that menu.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

I wouldn't say putting data in a view bag is a best practice. I would say using a view model is a best practice.

Suppose you want to add object to every view but not necessarily pass it to view the model (since there may not even be a model for a given view).

Even if you haven't define a view model, conceptually there is a model is you are passing data to the view.

See Why do we use ViewModels?

If the data is on every view, you could create a base view model that every model inherits from that contains the universal data.

you could also create a partial view with its own view model that is then applied to each page.

Community
  • 1
  • 1
Fran
  • 6,440
  • 1
  • 23
  • 35
0

Normally I design ViewModels taking in consideration the business or functional concepts of the application. For example, if you are displaying an invoice, the view model should contain all the properties related to invoices that needs to be displayed on that specific view. I wouldn't use ViewBags/ViewData for this purposes.

On the ViewBag I normally put other and "more ancillary" properties like, for example, a boolean for showing/hiding some part of the view, or maybe for some other "not business relevant" properties.

Of course some people could disagree with this and would put everything on the Viewmodels, some others not. This is a decision you must take at the beginning so you can be consistent in the way you design across the application.

Javier
  • 2,093
  • 35
  • 50