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 6792273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:56:56+00:00 2026-05-26T17:56:56+00:00

Could any body explain, when to use TempData ViewBag ViewData I have a requirement,

  • 0

Could any body explain, when to use

  1. TempData
  2. ViewBag
  3. ViewData

I have a requirement, where I need to set a value in a controller one, that controller will redirect to Controller Two and Controller Two will render the View.

I have tried to use ViewBag, the value gets lost by the time I reach Controller Two.

Can I know when to use and advantages or disadvantages?

Thanks

  • 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-05-26T17:56:57+00:00Added an answer on May 26, 2026 at 5:56 pm

    1)TempData

    Allows you to store data that will survive for a redirect. Internally it uses the Session as backing store, after the redirect is made the data is automatically evicted. The pattern is the following:

    public ActionResult Foo()
    {
        // store something into the tempdata that will be available during a single redirect
        TempData["foo"] = "bar";
    
        // you should always redirect if you store something into TempData to
        // a controller action that will consume this data
        return RedirectToAction("bar");
    }
    
    public ActionResult Bar()
    {
        var foo = TempData["foo"];
        ...
    }
    

    2)ViewBag, ViewData

    Allows you to store data in a controller action that will be used in the corresponding view. This assumes that the action returns a view and doesn’t redirect. Lives only during the current request.

    The pattern is the following:

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

    and in the view:

    @ViewBag.Foo
    

    or with ViewData:

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

    and in the view:

    @ViewData["Foo"]
    

    ViewBag is just a dynamic wrapper around ViewData and exists only in ASP.NET MVC 3.

    This being said, none of those two constructs should ever be used. You should use view models and strongly typed views. So the correct pattern is the following:

    View model:

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

    Action:

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

    Strongly typed view:

    @model MyViewModel
    @Model.Foo
    

    After this brief introduction let’s answer your question:

    My requirement is I want to set a value in a controller one, that
    controller will redirect to ControllerTwo and Controller2 will render
    the View.

    public class OneController: Controller
    {
        public ActionResult Index()
        {
            TempData["foo"] = "bar";
            return RedirectToAction("index", "two");
        }
    }
    
    public class TwoController: Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Foo = TempData["foo"] as string
            };
            return View(model);
        }
    }
    

    and the corresponding view (~/Views/Two/Index.cshtml):

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

    There are drawbacks of using TempData as well: if the user hits F5 on the target page the data will be lost.

    Personally I don’t use TempData neither. It’s because internally it uses Session and I disable session in my applications. I prefer a more RESTful way to achieve this. Which is: in the first controller action that performs the redirect store the object in your data store and user the generated unique id when redirecting. Then on the target action use this id to fetch back the initially stored object:

    public class OneController: Controller
    {
        public ActionResult Index()
        {
            var id = Repository.SaveData("foo");
            return RedirectToAction("index", "two", new { id = id });
        }
    }
    
    public class TwoController: Controller
    {
        public ActionResult Index(string id)
        {
            var model = new MyViewModel
            {
                Foo = Repository.GetData(id)
            };
            return View(model);
        }
    }
    

    The view stays the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anybody explain in plain words how Cloud computing works? I have read the
Could any body offer me any reason about that? If we do it like
Is anybody could explain when and why we should use System.ComponentModel.Container , please? Recently
Could some body explain me caching in asp.net? I am just confused on the
Could anybody explain what is the cause of the following: >>> from M2Crypto import
Could anybody explain to me the process of uploading to and downloading form and
Can anybody please explain how this could possibly happen? I am completely aware of
Could anybody brief about user_token functionality in Auth module? What is a use and
there is a list of ruby gems for twitter could anybody recommend me one
I'll try and explain the best I can... I have a page, we'll call

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.