DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Foundations for Building an Apache Flink Application
  • GDPR Compliance With .NET: Securing Data the Right Way
  • An Introduction to Object Mutation in JavaScript
  • Understanding the Differences Between Repository and Data Access Object (DAO)

Trending

  • Docker Base Images Demystified: A Practical Guide
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • Java’s Next Act: Native Speed for a Cloud-Native World
  1. DZone
  2. Data Engineering
  3. Data
  4. Session Management in ASP.NET MVC

Session Management in ASP.NET MVC

In this article, a developer discusses three different ways to deal with session management in ASP.NET MVC and provides some code for each.

By 
Amar Srivastava user avatar
Amar Srivastava
·
Feb. 08, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
42.4K Views

Join the DZone community and get the full member experience.

Join For Free

Difference Between ViewData, ViewBag, and TempData With Code Examples

In this article, I will explain, with an example, the similarities and differences between ViewData, ViewBag, and TempData in ASP.NET MVC.

ViewData, ViewBag and TempData are used for transferring data and objects from the Controller to the View or from one Controller to another in ASP.NET MVC.

ViewData

  1. ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object, i.e. it has keys and values where keys are strings and Values are objects.

  2. Data is stored as an Object inViewData.

  3. While retrieving data, the data needs to be Type Cast to its original type, as it will be stored as an object. ViewData also requires NULL checks while retrieving data.

  4. ViewData is used for passing a value from the Controller to the View.

  5. ViewData is available only for Current Requests. It will be destroyed upon redirection.

Example

In the below example, a string value is set in the ViewData object in the Controller and it is then displayed in the View.

Controller

public class FirstController: Controller {
 // GET: First
 public ActionResult Index() {
  ViewData["Message"] = "Hello Sultan!";
  return View();
 }
}

View

<html>
 <head>
  <meta name="viewport" content="-width"/>
  <title>Index</title>
 </head>
<body>
 <div>
  @ViewData["Message"]
  </div>
 </body>
</html>

ViewBag

  1. ViewBag is a Wrapper built around ViewData.

  2. ViewBag is a dynamic property and it makes use of C# 4.0's dynamic features.

  3. While retrieving data, there is no need for Type Casting the data.

  4. ViewBag is used for passing a value from the Controller to the View.

  5. ViewBagis available only for Current Requests. It will be destroyed upon redirection.

Example

In the below example, a string value is set in the ViewBag object in the Controller and it is then displayed in the View.

Controller

public class FirstController: Controller {
 // GET: First
 public ActionResult Index() {
  ViewBag.Message = "Hello Sultan!";
  return View();
 }
}

View

<html>
 <head>
  <meta name="viewport" content="-width"/>
  <title>Index</title>
 </head>
<body>
 <div>
  @ViewBag.Message
 </div>
</body>
</html>

TempData

  1. TempData is derived from the TempDataDictionary class and is basically a Dictionary object, i.e. keys and values where keys are string while values will be objects.

  2. Data is stored as an object in TempData.

  3. While retrieving, the data it needs to be Type Casted to its original type as the data is stored as objects and it also requires NULL checks while retrieving data.

  4. TempData can be used for passing a value from the Controller to the View and also from the Controller to another Controller.

  5. TempData is available for current and subsequent requests. It will not be destroyed on redirection.

Example

In the below example, a string value is set in the TempData object in the Controller and it is redirected to another Controller and, finally, it is displayed in the View.

First Controller

public class FirstController: Controller {
  // GET: First
  public ActionResult Index() {
   TempData["Message"] = "Hello Sultan!";
   return new RedirectResult(@ "~\Second\");
   }
  }

Second Controller

public class SecondController: Controller {
 // GET: Second
 public ActionResult Index() {
  return View();
 }
}

View of Second Controller

<html>
 <head>
    <meta name="viewport" content="-width"/>
    <title>Index</title>
 </head>
<body>
 <div>
  @TempData["Message"];
 </div>
</body>
</html>
ASP.NET MVC ASP.NET Object (computer science) Data (computing) Session (web analytics)

Opinions expressed by DZone contributors are their own.

Related

  • The Foundations for Building an Apache Flink Application
  • GDPR Compliance With .NET: Securing Data the Right Way
  • An Introduction to Object Mutation in JavaScript
  • Understanding the Differences Between Repository and Data Access Object (DAO)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!