Like we have state management in ASP.NET, ViewBag and ViewData are also used for providing the state management in MVC
Use of ViewBag and ViewData:
1 It helps to maintain data when you move from controller to view.
2 It is used to pass data from controller to corresponding view.
In View
public ActionResult Index()
{
ViewBag.Name = "Himanshu Joshi";
return View();
}
public ActionResult Index()
{
ViewData["Name"] = "Himanshu Joshi";
return View();
}
For retrieving values in forms
@ViewBag.Name
@ViewData["Name"]
0 Comment(s)