Tuesday 11 August 2015

ViewData & ViewBag

ViewBag & ViewData helps to maintain data when you move from controller to corresponding view.Its value becomes null when redirection occurs.

ViewData and ViewBag are almost similar. You can say that, viewData has been put in a wrapper and is now caller Viewbag.
What does that wrapper do  - Gives dynamic feature, no typecasting or null checks required(which is essential for ViewData)

public ActionResult Index()
{
    ViewBag.Name = "Prabhat Padhy";
    return View();
}
public ActionResult Index()
{
    ViewData["Name"] = "Prabhat Padhy";
    return View();
}

No comments:

Post a Comment