Premium Only Content

Part 6 ViewData and ViewBag in mvc
In this video we will discuss
1. What is ViewData
2. What is ViewBag
3. Difference between ViewData and ViewBag
Both ViewData and ViewBag are used to pass data from a controller to a view. ViewData is a dictionary of objects that are stored and retrieved using strings as keys. The syntax of ViewData is very similar to that of ViewState, SessionState and ApplicationState.
// Storing data in ViewData
ViewData["YourData"] = "SomeData";
// Retrieving data from ViewData
string strData = ViewData["YourData"].ToString();
ViewData does not provide compile time error checking. For example, if you mis-spell the key names you wouldn't get any compile time error. You get to know about the error only at runtime.
ViewBag uses the dynamic feature that was introduced in to C# 4. It allows an object to have properties dynamically added to it. Using ViewBag the above code can be rewritten as below.
// Storing data in ViewBag
ViewBag.YourData = "SomeData";
// Retrieving data from ViewBag
string strData = ViewBag.YourData;
Just like ViewData, ViewBag does not provide compile time error checking. For example, if you mis-spell the property name, you wouldn't get any compile time error. You get to know about the error only at runtime.
Internally ViewBag properties are stored as name/value pairs in the ViewData dictionary.
Please Note: To pass data from controller to a view, It's always a good practice to use strongly typed view models instead of using ViewBag & ViewData. Strongly typed view models provide compile time error checking. We will discuss view models in a later video session.
-
LIVE
Dr Disrespect
3 hours agoLIVE - DR DISRESPECT - MARVEL RIVALS, PUBG, OFF THE GRID - TRIPLE THREAT GAME CHALLENGE
1,727 watching -
LIVE
Tucker Carlson
1 hour agoBill Gates, Truth About Vaccines, & Big Pharma’s Plot to Destroy Doctors Who Question ”The Science”
5,189 watching -
20:08
Professor Nez
24 minutes ago🚨🔥BRUTAL! Trump ROASTS Newsom SO BAD it BREAKS Gavin for Good
1 -
2:04:38
Side Scrollers Podcast
3 hours agoEveryone HATES Baseball Karen + Gaming’s Newest Virtue Signal + MORE | Side Scrollers Live
9701 -
LIVE
StoneMountain64
1 hour agoDelta Force Budget vs JUICER Loadouts
112 watching -
LIVE
Sean Unpaved
2 hours agoYardline Yarns: Giants Need Juice, Bills-Ravens Classic, Dolphins' Soft Spot & Packers Prove It
144 watching -
1:05:03
Timcast
2 hours agoDemocrat media COVERS UP Irina Zarutska Murder, Second White Woman Killed, Trump Says WAR
79.3K77 -
2:07:35
Steven Crowder
5 hours agoThe Murder of A Ukrainian Refugee is A Tipping Point in American History
277K291 -
1:03:37
The Rubin Report
3 hours agoMedia Caught Trying to Ignore Ugly New Details of Charlotte Train Stabbing Caught on Tape
36K72 -
1:25:59
Rebel News
1 hour agoConservative MP rips Doug Ford, Carney's decarbonization scheme, Security cam rules | Rebel Roundup
5.18K1