How ViewBag Works in ASP.NET MVC

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

Also, how can I create it and use it in my ASP.NET WebForms application?

Examples would be greatly appreciated!

+85
asp.net-mvc viewbag
Feb 15 '13 at 13:39
source share
6 answers

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

It is declared as follows:

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

therefore you can:

ViewBag.Foo = "Bar";

Expander 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; } } } 
+74
Feb 20 '13 at 17:15
source share

This is a dynamic object, that is, you can add properties to it in the controller and read them later in the view, because you essentially create an object like you, a feature of the dynamic type. See the MSDN article on dynamics. See this article on MVC usage.

If you want to use this for web forms, add a dynamic property to the base page class, for example:

 public class BasePage : Page { public dynamic ViewBagProperty { get; set; } } 

Eliminate all your pages. You should be able to in your ASP.NET markup:

 <%= ViewBagProperty.X %> 

That should work. If not, there are ways around this.

+31
Feb 15 '13 at 13:43
source share

ViewBag is System.Dynamic.ExpandoObject as suggested. Properties in the ViewBag are essentially KeyValue pairs, where you access the value with a key. In this sense, they are equivalent:

 ViewBag.Foo = "Bar"; ViewBag["Foo"] = "Bar"; 
+5
Feb 15 '13 at 13:42
source share

ViewBag is used to transfer data from the controller action to view to transfer the transmitted data. Now you can transfer data using the Controller Action and View, either using the ViewBag or ViewData. ViewBag: type of dynamic object, which means that you can add new fields for dynamic viewing and access these fields in the view. You need to initialize the viewbag object while creating new fields.

for example: 1. Create a ViewBag: ViewBag.FirstName = "John";

  1. Access to view: @ ViewBag.FirstName.
+4
Nov 20 '15 at 4:30
source share

ViewBag is a dynamic type. Moreover, you cannot make ViewBag["Foo"] . You will get an exception. Indexing with [] cannot be applied to an expression like "System.Dynamic.DynamicObject".

The internal implementation of the ViewBag actually stores the Foo in the ViewData["Foo"] (type ViewDataDictionary), so these 2 are interchangeable. ViewData["Foo"] and ViewBag.Foo .

And the area. ViewBag and ViewData are designed to transfer data between the actions of the Controller and View it render.

+2
Feb 15 '13 at 13:52
source share
 public dynamic ViewBag { get { if (_viewBag == null) { _viewBag = new DynamicViewData(() => ViewData); } return _viewBag; } } 
-one
Dec 28 '16 at 6:11
source share



All Articles