Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();     if(obj.Valid)     {         someMessage = obj.UserName;     }     else     {         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

  • When to use TempData vs Session in ASP.Net MVC
  • http://www.gregshackles.com/2010/07/asp-net-mvc-do-you-know-where-your-tempdata-is/

Thanks

like image 285
Yasser Shaikh Avatar asked Sep 14 '12 10:09

Yasser Shaikh


People also ask

When should we use TempData in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.

Which is better TempData or session in MVC?

TempData is ideal for short-lived things like displaying validation errors or other messages that we don't need to persist for longer than a single request. Session is ideal choice when we need to persist data for extended periods of time i.e. it is used to store data which is required throughout user session.

How long does TempData last in MVC?

ASP.net MVC will automatically expire the value of tempdata once consecutive request returned the result (it means, it alive only till the target view is fully loaded).

How use TempData with keep in MVC?

The Keep function is used to preserve the data of TempData object even after the value is read while the Peek function is used to read the value without clearing it. When value is read from the TempData object, the value is cleared and NULL value is assigned. TempData object value was read in View and hence it is NULL.


2 Answers

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.

like image 162
Display Name Avatar answered Oct 10 '22 23:10

Display Name


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. https://msdn.microsoft.com/en-in/library/dd394711%28v=vs.100%29.aspx

like image 20
Yogi Avatar answered Oct 10 '22 23:10

Yogi