Download presentation
Presentation is loading. Please wait.
Published byAda Majanlahti Modified over 6 years ago
1
Data Structures and Database Applications View and Session Data
2
ViewBag, ViewData, and TempData
In ASP.NET MVC there are four objects available to pass data to views ViewBag - For passing data from a controller to a view ViewData TempData For passing data during redirects Session For passing data between Action Methods but can also be used for views Data persists for entire session
3
ViewBag When you use ViewBag data you get
Access to a ViewBag objects only last during the current request Access to a ViewBag object does not require typecasting for getting data In Controller Ex: ViewBag.Message = "Some Value"; In View Ex: </h2>
4
ViewData When you use ViewData data you get
Access to a ViewData objects only last during the current request Access to a ViewData object can require typecasting for getting data that is not a string In Controller Ex: ViewData["Message"] = "Some Value"; In View Ex: </h2>
5
TempData When you use TempData data you get
Access to a TempData objects last during a redirect from one view to another, or one view state to another Access to a TempData object can require typecasting for getting data that is not a string In Controller Ex: TempData["ErrorMessage"] = "Some Error"; In View Ex: </h2>
6
Session When you use Session data you get
Access to a Session objects last during the life of the user’s session (about 20 minutes when not authenticated). Access to a Session object does require typecasting for getting data that is not a string Session is mainly for method-to-method message passing, but can be used for method-to-view In Controller Ex: Session["Message"] = "Some Value"; In View Ex: Session["Message"] </h2>
7
Raw Data If you put HTML in any one of the View or Session fields you will need to use the HTML.Raw() helper method with the field if you want to maintain the HTML formatting:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.