?
Solved

Razor: Question about this dynamic object, ViewBag

Posted on 2011-10-18
2
Medium Priority
?
767 Views
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...

Thanks.
0
Comment
Question by:newbieweb
2 Comments
 
LVL 27

Accepted Solution

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

http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx
http://davidhayden.com/blog/dave/archive/2011/01/19/ViewBagAspNetMvc3.aspx

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:

<title>@Model.Title</title>

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)
{
     <text>@name</text>
}

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).

Arun
0
 

Author Closing Comment

by:newbieweb
ID: 36993508
Thanks!
0

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