Introduction to
passing data in .NET
Learn about the different methods of passing data from the model to the
view in .NET.
What is the Model in .NET?
1 Data Management 2 Database 3 Object
The Model handles the Interaction Representation
data, business logic, and It serves as the interface Represents the
data manipulation. for interaction with the application's data
database. structure and behavior.
What is the View in .NET?
User Interface Display Logic
The View represents the user interface Handles the display of the data to the
of the application. user.
Interactivity
It enables user interactions and actions within the application.
How does data pass from Model to
View?
1 2 3
ViewData ViewBag TempData
Dictionary-like object for Dynamic wrapper around Enables passing data
passing data using key-value ViewData for convenient between controller actions,
pairs. data passing. particularly during redirects,
for short-term storage.
Examples of Data Passing
1 View Data Usage
Demonstration of View Data-based data passing process.
2 View Bag Implementation
Examples showcasing the usage of View Bag for data transfer.
3 TempData
A dictionary-like structure used to store data temporarily between controller actions,
typically during redirects.
ViewData
Functionality Loose Typing
Allows passing data from controllers to views Requires casting the data when retrieved in the
using key-value pairs. view.
Output
ViewBag
1 Dynamic Nature 2 Convenient Syntax
Provides a dynamic wrapper around Offers a more convenient and concise
ViewData. syntax for data passing.
Output
TempData
Short-term Data Persistence Type Safety
TempData allows you to store data that needs TempData supports strong typing. This
to persist for a short duration, typically across means you can specify the type of data you're
redirects. storing in TempData, providing compile-time
type safety and reducing the risk of runtime
errors related to data type mismatches.
Output