1:-ViewData
- ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
- ViewData requires typecasting for complex data type and check for null values to avoid error
Example:-
public ActionResult Index()
{
ViewData["Name"] = "Dhiren Web developer";
return View();
}
2:-ViewBag
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- ViewBag doesn’t require typecasting for complex data type.
Example:-
public ActionResult
Index()
{
ViewBag.Name = "Dhiren Web developer";
return View();
}
Calling in View
@ViewBag.Name
@ViewData["Name"]
Hope you like it.
Stay tuned for more..
Comments
Post a Comment