Data Passing Techniques in ASP.NET MVC

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

Data Passing Technique in ASP.NET MVC

In ASP.NET MVC, data is typically sent from the controller to the view. The controller prepares the data, which is subsequently delivered to the view for presentation.

Model

This is the primary method for data transfer between controllers and views. Models are classes that hold the data for your application. You can pass a model instance to the view using the controller's ViewData property. The model's data can then be accessed by the view via the model attributes.

ViewData

In ASP.NET MVC, ViewData is a dictionary object that contains values in key-value pairs and is used to transport data between controllers and views. Typecasting is required to get data in C#, and saved values become null if a redirection happens.

ViewBag

ViewBag, first introduced in ASP.NET MVC 3, is a dynamic property that wraps ViewData and is used to convey data from the controller to the view. It does not require typecasting to obtain data in C#, making it easier to use than ViewData.

TempData

TempData is a dictionary object that contains values in key-value pairs and is used to transmit data between requests. To get data in C#, typecasting is required, and values are stored internally in the session.

Session

The session, a Controller class property of type HttpSessionStateBase, is used to pass data within an ASP.NET MVC application. It stores data for a user's session and requires typecasting to obtain it in C#.

QueryString

Query strings are used to send data within an MVC application via URL parameters. They are not recommended for transmitting sensitive information because to security issues and URL length restrictions.

Cookies

Cookies are used to save data on the client side of an MVC application. Persistent cookies are saved as a text file on the client's computer, whereas non-persistent cookies are kept in the browser's memory.

ViewModel

A ViewModel is a custom class in ASP.NET MVC that aggregates numerous models and additional data needed by the view. It preserves a clear separation of concerns while also providing compile-time and type safety.

Forms (Post Data)

Forms in ASP.NET MVC are used to send data from the client to the server through form submission. This approach is beneficial for larger datasets since it retains state between requests; nevertheless, it requires sufficient security measures to prevent attacks such as CSRF.

Hidden fields

Hidden fields allow you to embed data into HTML forms. Hidden fields are form components of type hidden that are not visible to the user but are submitted with the form data.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this