6

I am a student working on a mvc project to move a site from coldfusion to .net mvc. Were are also redesigning the site in the process so I am working with other team members, the design team members have a basic understanding of razor, but aren't expected to know any other parts of the framework.

I've always tried to use ViewBag as little as possible, now even more so because the designers don't know what is in the ViewBag when building the views, and get no intellisense.

Since working on this project I've moved to using the ViewBag only in the baseController for the project adding properties in the OnActionExecuted So that every view will have the same ViewBag Properties. I've also tried to only use ViewBag properties in layouts.

Since doing this I've wondered what are good reasons for ever adding to the ViewBag in a controller action directly before calling the view. I've come to appreciate that making more ViewModels while slightly more time helps to inform exactly what a view should be doing, as well as adds clarity.

I wanted to know when other people used ViewBag where they found it appropriate and what they used to it for. I can't think of using it any time outside of a basecontroller now, I was hoping to get insight into other uses for the ViewBag that perhaps I am just missing. Should I ever be adding to the ViewBag directly before calling an action, or should I create a more robust viewModel in these cases?

  • FWIW I had some subtle bugs happen because of I miss typed a property name on a ViewBag. You get zero indication that anything is wrong unless you happen to visually see something missing on the page. Too easy to miss. Too easy to go wrong. I just don't use it. – RubberDuck Dec 9 '16 at 0:17
6

A ViewBag is essentially a Dictionary, except that the syntax is a bit cleaner. Instead of writing

myDict["SomeProperty"]

you can access the dynamic dictionary's elements with first-class dot notation:

myViewBag.SomeProperty

Because ViewBag is a property of ControllerBase, it provides a convenient place for storing key/value pairs during controller operations, using a first-class syntax. You would use it anytime you need to send key/value pairs to a View, and you want the convenience and clean syntax of a dynamic dictionary.

ViewBag derives from DynamicViewData, which gives it some useful properties. For example:

// Implementing this function extends the ViewBag contract, supporting or improving some scenarios. For example
// having this method improves the debugging experience as it provides the debugger with the list of all
// properties currently defined on the object.
public override IEnumerable<string> GetDynamicMemberNames()
{
    return ViewData.Keys;
}

In general, I would favor strongly-typed ViewModel objects over the ViewBag or ViewData dictionaries, since strongly-typed objects give you compile-time type safety. This is especially true if you have complex structured data to pass to the View.

See Also
ExpandoObject

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.