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.
-
9:39
Tactical Advisor
23 hours agoHow To Improve Your Shooting For FREE! Mantis Blackbeard X
5.04K4 -
1:00:59
Squaring The Circle w/ Randall Carlson
19 hours ago#007 The Real Climate Crisis Noone Is Talking About and Mount Tambora Pt 2
6.53K4 -
13:52
America Uncovered
20 hours agoEveryone is WRONG About Trump's Guilty Verdict
4.41K15 -
13:10
Censored TV
19 hours agoThe Owen Benjamin vs Jim Goad debate was WILD
2.13K11 -
25:27
Degenerate Plays
11 hours agoThe Microtransaction Master - Call Of Duty Modern Warfare Remastered : Part 3
4.11K2 -
21:03
MYLUNCHBREAK CHANNEL PAGE
14 hours agoOld World Minnesota?
8.51K15 -
5:49:05
Akademiks
20 hours agoDrake Next Move - Whats next?? Did Lil Baby Listen to Me? Diddy and Family Preparing for Indictment?
62.6K29 -
1:56:06
TimcastIRL
1 day agoTrump Raises RECORD $52.8 MILLION In One Day, Bonus Uncensored Show w/Laura Loomer | Timcast IRL
83.5K359 -
23:19
Scammer Payback
4 days agoWe Created the First Ever 𝗔𝗡𝗧𝗜-𝗦𝗖𝗔𝗠 Call Center
208K246 -
1:11:09
LFA TV
22 hours agoTRUMP GUILTY…OF LOVING AMERICA! Ft. Hayley Caronia & Vish Burra | LAST CALL 6.1.24 8PM EST
72.2K32