Passing Data From Controllers to Views (ViewBag & ViewData) – Session 19

ViewBag & View Data - Session 19

In this session of ASP.NET Core Tutorial, we are going to talk about passing data from Controllers to Views with ViewBag and ViewData. There is two different Views in terms of passing and processing data, Dynamic View and Strongly Typed View. In this session we describe both and show you passing data in Dynamic View.

ViewBag Vs ViewData

  • Both ViewBag and ViewData are using for passing data from Controllers to Views
  • Both are using for passing data to Dynamic View (Weakly Typed View)
  • ViewBag uses Dynamic Property to store/restore data while ViewData uses String Key
  • Passed data to Views with ViewBag doesn’t need data casting while data except String type needs data casting when data transferred with ViewData
  • Visual Studio doesn’t provide IntelliSense and Combile Time Error for both method.

Dynamic View Vs Strongly Typed View

Dynamic View (Weakly Typed View) is type of View which isn’t bound to any type of Models. Visual Studio doesn’t provide IntelliSense and Compile Time Error for this type of View. Thus, there is possibility of Run Time Error due to typos.

On the other hand, Strongly Typed View is type of View that is bound to a specific type of Model. Visual Studio provides IntelliSense and Compile Time Error for this type of View. This type of View is highly recommended and we will talk about it in further sessions.

ViewBag

One of the method that we can pass data from Controllers to Dynamic Views, is using of ViewBag. ViewBag uses Dynamic Property to store and restore data. Also, we do not need to cast data which is passed to View by ViewBag.

ViewData

Another way of passing data from Controllers to Weakly Typed Views is using ViewData. ViewData uses String Key for store and restoring data. Moreover data except String data should be casted in this method.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube and you can have access to list of all sessions HERE.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes

Tags

5 1 vote
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments