Razor: Question about this dynamic object, ViewBag

Posted on 2011-10-18
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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now