Razor: Question about this dynamic object, ViewBag

Posted on 2011-10-18
Medium Priority
Last Modified: 2012-05-12
I use this format but do not realy understand it as well as I'd like to.

@model MyModel
  ViewBag.Title = "My Window";

Clearly, the model used for this view is called MyModel.

Please describe the ViewBag and what else it can be used for besides setting the title.  There are other ways to set the title of a Razor file, so what are the benefits of this method?

I though that using a dynamic object and a dynamic property were not advisable unless needed explicitly. Please explain...

Question by:newbieweb
LVL 27

Accepted Solution

nmarun earned 2000 total points
ID: 36990724
Below are a couple of reads about ViewBag.


You seem to be setting the title of the page. Here is how I would do it and this is also the way to separate logic from data.

Add a property called Title in your MyModel class, this way you can set the title of the page using your own logic. In your view, all you'll have to do is:


By doing this, your view remains pretty dumb (used only to display data, not to do some "logic" work) while all your logic is happening in the back-end.

Coming to ViewBag, it can be used to set up pretty much anything. In your code behind (say an action method), you can do
ViewBag.Names = new List<string>{"ScottGu", "Hanselman", "Haack"};
and in your view you can say:
@foreach(string name in ViewBag.Names)

Although ViewBag can have pretty much anything in it, you have to rely on having all these things in your model object. This will help you build a good Model and will allow you to do write better unit tests around the Model object (not saying you cannot write unit tests against ViewBag).


Author Closing Comment

ID: 36993508

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question