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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 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)
  • JSON-Based Serialized LOB Pattern

Trending

  • AI-Driven RAG Systems: Practical Implementation With LangChain
  • The Middleware Gap in AI Agent Frameworks
  • Alternative Structured Concurrency
  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  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.7K 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

  • 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)
  • JSON-Based Serialized LOB Pattern

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook