View
data:
is a dictionary used to store data between View and controller; you need to
cast the view data object to its corresponding model in the view to be able to
retrieve data from it
View Bag: is a dynamic property similar in its working to the view data, however it is better it doesn't need to be casted to its corresponding model before using it in the view
Diversity between View Data and View Bag
View Data is a dictionary of objects that is derived from View data Dictionary class and accessible using strings as keys.
View
Bag & View Data Example:
Public ActionResult Index ()
{
ViewBag.Name = "xyz";
return View();
}
Public ActionResult Index ()
{
View Data["Name"] = " xyz ";
return View();
}
Calling in View
@ViewBag.Name
@View Data ["Name"]
View Bag: is a dynamic property similar in its working to the view data, however it is better it doesn't need to be casted to its corresponding model before using it in the view
Diversity between View Data and View Bag
View Data is a dictionary of objects that is derived from View data Dictionary class and accessible using strings as keys.
View Bag is a dynamic property that takes
advantage of the new dynamic features in C# 4.0.
View Data
requires typecasting for complex data type and check for null values to avoid
error.
View Bag
doesn’t require typecasting for complex data type.
Public ActionResult Index ()
{
ViewBag.Name = "xyz";
return View();
}
Public ActionResult Index ()
{
View Data["Name"] = " xyz ";
return View();
}
Calling in View
@ViewBag.Name
@View Data ["Name"]
No comments:
Post a Comment