How ViewBag in ASP.NET MVC works

前端 未结 7 2078
清酒与你
清酒与你 2020-11-28 04:38

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

7条回答
  •  不知归路
    2020-11-28 05:18

    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.

提交回复
热议问题