Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between ViewData & PageData in asp.net MVC 3?

Well i see this 2 properties but i cant understand the difference between them?
I cant seem to find any help anywhere about the PageData propriety.
so can any body help?

@
{
 Viewdata["something"] = 1;
 PageData["something"] = 2;
}

thanks

like image 726
Karim Avatar asked Jun 21 '11 16:06

Karim


People also ask

What is difference between ViewData and TempData?

In one sentence: TempData are like ViewData with one difference: They only contain data between two successive requests, after that they are destroyed. You can use TempData to pass error messages or something similar. Although outdated, this article has good description of the TempData lifecycle.

What are the differences between ViewData ViewBag TempData and Session?

In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session.

When should we use ViewData?

All three objects are available as properties of both the view and controller. As a rule of thumb, you'll use the ViewData, ViewBag, and TempData objects for the purposes of transporting small amounts of data from and to specific locations (e.g., controller to view or between views).

Which one is faster ViewBag or ViewData?

Requires typecasting for complex data type and checks for null values to avoid error. If redirection occurs, then its value becomes null. ViewData is faster than ViewBag.


2 Answers

PageData is a property of WebPages that Razor is built on

[it] Provides array-like access to page data that is shared between pages, layout pages, and partial pages.

http://msdn.microsoft.com/en-us/library/system.web.webpages.webpagebase.pagedata(v=VS.99).aspx

ViewData is a property of ViewPage and provides a method of passing data between a controller and a page.

Gets or sets a dictionary that contains data to pass between the controller and the view.

http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.viewdata.aspx

ViewData is accessible via the controller, PageData isn't.

like image 180
David Glenn Avatar answered Sep 28 '22 03:09

David Glenn


I'm not 100% sure, but it looks like PageData is something from WebMatrix (MS's "light" web development environment). And ViewData is something that is fully supported in MVC. ViewData is in the System.Web.Mvc namespace, and therefore any MVC app will have it available.

PageData is apparently (according to MSDN) in the System.Web.WebPages namespace, but I can't access PageData from my MVC controllers, even if I fully qualify it. Maybe it's not meant to be accessed from the controller side, like ViewData can be...?

There's some more info on PageData in this other StackOverflow question here, but info on it around the web is surprisingly sparse (as you've probably found).

I hope that helps!

like image 20
dizzwave Avatar answered Sep 28 '22 03:09

dizzwave