This article contain following things in brief
1. What is ViewBag
2. What is ViewData
3. Difference between ViewData and ViewBag
Both ViewBag and ViewData 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.
ViewBag:-ViewBag uses the dynamic feature that was introduced in to C# 4.0. It allows an object to have properties dynamically added to it.
// Storing data in ViewBag
ViewBag.YourData = "SomeData";
// Retrieving data from ViewBag
ViewData :-
ViewData["YourData"] = "SomeData";// // Storing data in ViewData
ViewData["YourData"]// Retrieving data from ViewData.
Just like ViewBag, ViewData does not provide compile time error checking. e.g, if you misspell the property name, you wouldn't get any compile time error. You get to know about the error only at run time.
Internally ViewBag properties are stored as name/value pairs in the ViewData dictionary.
1. What is ViewBag
2. What is ViewData
3. Difference between ViewData and ViewBag
Both ViewBag and ViewData 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.
ViewBag:-ViewBag uses the dynamic feature that was introduced in to C# 4.0. It allows an object to have properties dynamically added to it.
// Storing data in ViewBag
ViewBag.YourData = "SomeData";
// Retrieving data from ViewBag
ViewData :-
ViewData["YourData"] = "SomeData";// // Storing data in ViewData
ViewData["YourData"]// Retrieving data from ViewData.
Just like ViewBag, ViewData does not provide compile time error checking. e.g, if you misspell the property name, you wouldn't get any compile time error. You get to know about the error only at run time.
Internally ViewBag properties are stored as name/value pairs in the ViewData dictionary.
No comments:
Post a Comment