Using Tempdata in ASP.NET MVC - Best practice

I am using ASP.NET MVC 3 to build a web application.

What I am trying to do is pass values between two controllers, though there are many ways to do this I am particular interested in using TempData for this.

public ActionResult Action1()
    string someMessage;
    Test obj = SomeOperation();
        someMessage = obj.UserName;
        someMessage = obj.ModeratorName;

    TempData["message"] = someMessage;

    return RedirectToAction("Index");

public ActionResult Index()
    ViewBag.Message = TempData["message"]

    return View();

So is the use of TempData here correct ? I mean under best programming practices is this correct way of using TempData ?

In what real time cases should TempData be used ?

Note : I have gone through the following links



TempData is a bucket where you can dump data that is only needed for the following request. That is, anything you put into TempData is discarded after the next request completes. This is useful for one-time messages, such as form validation errors. The important thing to take note of here is that this applies to the next request in the session, so that request can potentially happen in a different browser window or tab.

To answer your specific question: there's no right way to use it. It's all up to usability and convenience. If it works, makes sense and others are understanding it relatively easy, it's good. In your particular case, the passing of a parameter this way is fine, but it's strange that you need to do that (code smell?). I'd rather keep a value like this in resources (if it's a resource) or in the database (if it's a persistent value). From your usage, it seems like a resource, since you're using it for the page title.

Hope this helps.

Please note that MVC 3 onwards the persistence behavior of TempData has changed, now the value in TempData is persisted until it is read, and not just for the next request.

The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

Just be aware of TempData persistence, it's a bit tricky. For example if you even simply read TempData inside the current request, it would be removed and consequently you don't have it for the next request. Instead, you can use Peek method. I would recommend reading this cool article:

MVC Tempdata , Peek and Keep confusion

Need Your Help

Use a string to call function in Python

python string function

Some days ago I was searching on the net and I found an interesting article about python dictionaries. It was about using the keys in the dictionary to call a function. In that article the author has

When using two frames in emacs, how do I prevent the compilation buffer from showing up in both?

emacs window compilation frame dot-emacs

I work with two monitors, and often use emacs with two frames open; one for each monitor. each frame is split into two side-by-side windows, like so: