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
Laura Loomer
2 hours agoEP145: Trump Makes BOMBSHELL Autism Announcement
848 watching -
LIVE
SpartakusLIVE
3 hours agoEXPLOSIVE 2v2 Tuesday has viewers GLUED to the screen
531 watching -
LIVE
GrimmHollywood
2 hours ago🔴LIVE • GRIMM'S TUESDAY FRIGHT NIGHT • STARRING GRIMM HOLLYWOOD • NO, I'M NOT HUMAN PART 1 •
93 watching -
LIVE
Flyover Conservatives
9 hours agoAI Encouraged Suicide: The Global Experiment on Our Kids - Joe Allen | FOC Show
289 watching -
1:07:40
Glenn Greenwald
4 hours agoTucker Carlson on Charlie Kirk Assassination Fallout, Free Speech, Foreign Policy, and the Reaction to his Kirk Remarks | SYSTEM UPDATE #520
135K100 -
14:22
Robbi On The Record
2 days ago $0.20 earnedGen Z’s Narcissism Obsession: Why Everyone’s a “Psychologist”
5.38K8 -
LIVE
GritsGG
5 hours agoQuad Win Streaks!🫡 Most Wins in WORLD! 3600+
156 watching -
1:09:28
Sarah Westall
2 hours agoCan the World Be This Strange? The Nature of Our Reality w/ Darius J Wright
7K -
megimu32
3 hours agoOn The Subject: Friends | 31 Years of the Sitcom That Defined a Generation
16.4K4 -
30:00
BEK TV
1 day agoCounter Culture Mom
1.55K