Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:53:12+00:00 2026-06-12T02:53:12+00:00

I was editing a project and I saw a Session[] in one controller method

  • 0

I was editing a project and I saw a Session[“”] in one controller method and TempData[“”] in another. Is there a difference between the 4 or is it just 4 ways to do the same thing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:53:13+00:00Added an answer on June 12, 2026 at 2:53 am
    • ViewData/ViewBag – valid only for the duration of the current request.
      You set it in a controller action and use it in the view, then it disappears.
      The difference is that the first is a dictionary whereas the second is just a dynamic wrapper around this dictionary.
      Both point to the same data though.
      ViewBag was introduced in ASP.NET MVC 3.

    Example:

    public ActionResult Index()
    {
        ViewData["foo"] = "bar";
        return View();
    }
    

    and inside the view you could use this value:

    <div>@ViewData["foo"]</div>
    

    Same with ViewBag but it is dynamic:

    public ActionResult Index()
    {
        ViewBag.foo = "bar";
        return View();
    }
    

    and inside the view you could use this value:

    <div>@ViewBag.foo</div>
    

    So as you can see ViewData/ViewBag are just an alternative way to pass information to a view from a controller action compared to the classic and recommended way which is using a view model:

    public class MyViewModel
    {
        public string Foo { get; set; }
    }
    

    and then:

    public ActionResult Index()
    {
        var model = new MyViewModel { Foo = "bar" };
        return View(model);
    }
    

    and inside your strongly typed view:

    @model MyViewModel
    <div>@Html.DisplayFor(x => x.Foo)</div>
    

    As you can see using view models provide a strongly typed approach in passing information to a view from a controller action.

    • TempData – it allows for persisting information for the duration of a single subsequent request. You store something inside TempData and then redirect. In the target controller action to which you redirected you could retrieve the value that was stored inside TempData.

    Example:

    public ActionResult Foo()
    {
        TempData["foo"] = "bar";
        return RedirectToAction("bar");
    }
    
    public ActionResult Bar()
    {
        var value = TempData["foo"] as string;
        // use the value here. If you need to pass it to the view you could
        // use ViewData/ViewBag (I can't believe I said that but I will leave it for the moment)
        return View();
    }
    

    ASP.NET MVC will automatically expire the value that was stored in TempData once you read it. Under the covers ASP.NET MVC persists the information into the Session.

    • Session – same as TempData except that it never expires – it will be valid for all requests, not a single redirect.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working in a project written by some one else ,there's a Function which
I am editing an existing project, and there was a bug being reported that
In Expression Blend 4, it's possible to load design-time resources by editing your project
I've started a directory project in C#.NET where I'm editing an xml file and
I'm building a game level editing app as part of a university project. In
I'm editing a piece of code, that is part of a big project, that
I'm currently editing a working project with some experience on PHP. I know a
I am editing a open source project called Opencv for Delphi which compiles fine
I'm working on a project that's trying to implement some editing features using a
I have a makefile in the project root directory. If I am editing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.