How ViewBag in ASP.NET MVC works

前端 未结 7 1984
清酒与你
清酒与你 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:21

    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.
    0 讨论(0)
提交回复
热议问题