105

How does the ASP.NET MVC's ViewBag work? MSDN says it is just an Object, which intrigues me, how does "Magic" properties such as ViewBag.Foo and magic strings ViewBag["Hello"] actually work?

Also, how can I make one and use it in my ASP.NET WebForms app?

Examples would be really appreciated!

Germstorm
  • 9,709
  • 14
  • 67
  • 83
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • The concept doesn't seem to be needed in WebForms as the page already has direct access to variables in it's code behind file. Whereas in MVC the view doesn't have access to anything in the controller apart from data that is passed in view the ViewBag or Model. – Richard Dalton Feb 15 '13 at 14:01

7 Answers7

92

ViewBag is of type dynamic but, is internally an System.Dynamic.ExpandoObject()

It is declared like this:

dynamic ViewBag = new System.Dynamic.ExpandoObject();

which is why you can do :

ViewBag.Foo = "Bar";

A Sample Expander Object Code:

public class ExpanderObject : DynamicObject, IDynamicMetaObjectProvider
{
    public Dictionary<string, object> objectDictionary;

    public ExpanderObject()
    {
        objectDictionary = new Dictionary<string, object>();
    }
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        object val;
        if (objectDictionary.TryGetValue(binder.Name, out val))
        {
            result = val;
            return true;
        }
        result = null;
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        try
        {
            objectDictionary[binder.Name] = value;
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 2
    Weird that [it just says `public object ViewBag { get; }` on the MSDN page for ViewBag](https://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.viewbag(v=vs.118).aspx), but I'm buying your claim that it's [an `ExpandoObject`](https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx). I like the [comparison with ViewData here](http://stackoverflow.com/a/4705446/1028230): "*So basically [ViewBag] replaces magic strings [in ViewData]... with magic properties*". ;^) – ruffin Feb 25 '16 at 21:28
  • @ruffin weirdly strange that it says `public object ViewBag { get; }` on MSDN, Code says it's [`dynamic ViewBag { get .. `](https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs) which internally is of type [DynamicViewData](https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/DynamicViewData.cs) – Aniket Inge Feb 26 '16 at 09:44
  • [Very nice](https://github.com/aspnet/Mvc/blob/8b1bd343ba56152d2a7e677cf041dc9d62b4033f/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs#L84). Thanks for digging that up. And I did +1 & say that I believed you! ;^) – ruffin Feb 26 '16 at 15:05
  • 2
    @ruffin another case of when the documentation lies and the code never lies – Aniket Inge Oct 12 '17 at 16:58
  • Sometimes I'm impressed when finding the right answers on not so clear/mixed questions. Thanks! – Oscar Ortiz Sep 18 '18 at 14:40
34

It's a dynamic object, meaning you can add properties to it in the controller, and read them later in the view, because you are essentially creating the object as you do, a feature of the dynamic type. See this MSDN article on dynamics. See this article on it's usage in relation to MVC.

If you wanted to use this for web forms, add a dynamic property to a base page class like so:

public class BasePage : Page
{

    public dynamic ViewBagProperty
    {
        get;
        set;
    }
}

Have all of your pages inherit from this. You should be able to, in your ASP.NET markup, do:

<%= ViewBagProperty.X %>

That should work. If not, there are ways to work around it.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
8

The ViewBag is an System.Dynamic.ExpandoObject as suggested. The properties in the ViewBag are essentially KeyValue pairs, where you access the value by the key. In this sense these are equivalent:

ViewBag.Foo = "Bar";
ViewBag["Foo"] = "Bar";
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
5

ViewBag is used to pass data from Controller Action to view to render the data that being passed. Now you can pass data using between Controller Action and View either by using ViewBag or ViewData. ViewBag: It is type of Dynamic object, that means you can add new fields to viewbag dynamically and access these fields in the View. You need to initialize the object of viewbag at the time of creating new fields.

e.g: 1. Creating ViewBag: ViewBag.FirstName="John";

  1. Accessing View: @ViewBag.FirstName.
Sachin Gaikwad
  • 331
  • 3
  • 10
3

ViewBag is of type dynamic. More, you cannot do ViewBag["Foo"]. You will get exception - Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'.

Internal implementation of ViewBag actually stores Foo into ViewData["Foo"] (type of ViewDataDictionary), so those 2 are interchangeable. ViewData["Foo"] and ViewBag.Foo.

And scope. ViewBag and ViewData are ment to pass data between Controller's Actions and View it renders.

Nenad
  • 24,809
  • 11
  • 75
  • 93
1

ViewBag is a dynamic type that allow you to dynamically set or get values and allow you to add any number of additional fields without a strongly-typed class They allow you to pass data from controller to view. In controller......

public ActionResult Index()
{
    ViewBag.victor = "My name is Victor";
    return View();
}

In view

@foreach(string a in ViewBag.victor)
{
     .........
}

What I have learnt is that both should have the save dynamic name property ie ViewBag.victor

Dexter
  • 69
  • 1
  • 10
-1
public dynamic ViewBag
{
    get
    {
        if (_viewBag == null)
        {
            _viewBag = new DynamicViewData(() => ViewData);
        }

        return _viewBag;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • 3
    I request you to please add some context/commentary around the code you have shared. It will help the asker and other future readers to understand your post better. – RBT Dec 28 '16 at 08:42