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.
-
52:00
Man in America
9 hours agoEXPOSED: The CIA's Darkest Secrets: Drugs, Coups, Mockingbird Media & Assassinations
33.5K3 -
59:53
Flyover Conservatives
23 hours agoBezos and Shifting Media Alliances You Didn’t See Coming! - Clay Clark; Early Voting May Lead to Trump Landslide - Mark Mitchell, Rasmussen Reports | FOC Show
32.8K5 -
1:10:37
Josh Pate's College Football Show
8 hours ago $0.20 earnedWeek 10 Upset Alerts | Ryan Day OR James Franklin | November Circle Games | Cole Cubelic Joins
15.6K6 -
2:17:12
Precision Rifle Network
1 day agoGuns & Grub S3E5 - Rex gets a new toy!
24K -
LIVE
VOPUSARADIO
12 hours agoPOLITI-SHOCK! LIVE W/ BIANCA DE LA GARZA & Dr. JEROME CORSI! ALSO, HARD HITTING TRUTH!!
770 watching -
1:31:55
Kim Iversen
7 hours agoTHIRD Trump Assassination Attempt Thwarted?!? | Ballot Glitches, Voters Turned Away By Cops, What's Going On?!?
76.3K66 -
1:26:31
Donald Trump Jr.
11 hours agoWe’re Just Days Away From Election Day & our MAGA Moment, Plus Interview with Sen Tom Cotton | TRIGGERED Ep.187
129K120 -
2:37:52
Laura Loomer
6 hours agoEP88: LIVE COVERAGE: Trump Rally in Henderson, NV
57.2K19 -
2:27:28
Slightly Offensive
6 hours ago $6.90 earnedJD Vance Did What?! BOMBSHELL Joe Rogan Interview EXPOSES ALL | Guest: Australian Talk
56.1K10 -
51:08
Kimberly Guilfoyle
10 hours agoGarbage Politics: Dems Lose Control, Live with Sage Steele, Asm Bill Essayli & Karoline Leavitt | Ep. 170
96.7K34