Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ASP.NET WebForms equivalent of ASP.NET MVC's ViewData

What are the conventions used in ASP.NET WebForm for passing data to view from code behind? In ASP.NET MVC for example ViewData is a key value collection or a strongly typed class object. So what do people do in case of ASP.NET WebForm.

I know we can create a property or member of a class or add stuff to Page.Items but what else besides that?

like image 840
Muhammad Hasan Khan Avatar asked Jun 27 '11 10:06

Muhammad Hasan Khan


People also ask

What is the equivalent of Web Forms in ASP.NET Core?

Solution #1: ASP.NET Core Razor Pages.

What is difference between ASP.NET webform and ASP.NET MVC?

Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.

What is ASP.NET ViewData?

ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa.

What is difference between TempData and ViewData?

To summarize, ViewBag and ViewData are used to pass the data from Controller action to View and TempData is used to pass the data from action to another action or one Controller to another Controller.


2 Answers

I think all the concepts of ASP.NET MVC do not map to ASP.NET Forms since they are two different paradigms of building web app.

In WebForms people mostly deal with controls and set their properties, they don't have to pass data to view as such. However if they do have to do so they use Page.Items or HttpContext.Current.Items or create Page properties that they access in views.

There is no direct equivalent of ViewData or ViewModel in WebForms that is used in practice. Page.Items is the closest thing.

like image 175
Muhammad Hasan Khan Avatar answered Oct 31 '22 13:10

Muhammad Hasan Khan


I'm not sure there is a direct equivalent, but the "HttpContext.Current.Items" collection can be accessed from anywhere without having to pass the context (though it does make assemblies dependent on System.Web).

like image 42
Swanny Avatar answered Oct 31 '22 13:10

Swanny