Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - TempData - Good or bad practice

I'm using the AcceptVerbs method detailed in Scott Gu's Preview 5 blog post for dealing with form entries in ASP.NET MVC:

  • User gets an empty form via GET
  • User posts the filled in form via POST to the same Action
  • The Action validates data, takes appropriate action, and redirects to a new view

So I don't have to use TempData. That said, I now have to add a 'confirm' step to this process, and it seems to require the use of TempData.

For some reason, I have an aversion to using TempData -- that it is something to be designed around.

Is this at all a valid concern, or am I making it up?

like image 493
anonymous Avatar asked Dec 22 '08 16:12

anonymous


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.

Is TempData safe?

Yes, TempData is backed by session storage, so if you are in a load balanced environment extra care must be taken when using it (sticky sessions, persistent session state, etc). TempData has been the de-facto choice when using the PRG pattern, and is what it was designed for.

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).


3 Answers

No need to have an aversion to TempData... But if not used correctly it could surely be an indication of poor design. If you are using RESTful URL's, TempData is a best practice for transfering messages from your POST Actions to your GET Actions. Consider this:

You have a form at URL Products/New. The form Posts to Products/Create, which validates the form and creates the Product, On Success the Controller redirects to URL Products/1 and on error would redirect back to products/New to display Error Messages.

Products/1 is just the standard GET action for the product, but we would like a message to display indicating the insert was a success. TempData is perfect for this. Add the message to TempData in the Post Controller and put some if logic in the view and your done.

On failure I've been adding the values entered in the formCollection and a collection of error Messages to TempData in the Post Action, and redirecting to the intial Action Prodcuts/New. I've added logic to the view to populate the form inputs with the previously entered values along with any error messages. Seems nice and clean to me!

like image 119
JasonD Avatar answered Oct 10 '22 12:10

JasonD


I think you do well to hesitate before using TempData. TempData is stored in the session and this may have implications for you if:

  1. You don't use sessions on your site right now
  2. You have a system that needs to scale to high throughput, i.e. you'd prefer to avoid session state altogether
  3. You don't want to use cookies (I don't know how well MVC supports cookieless sessions right now)

If your site needs to have high availability, then there are additional considerations around applying session state but these are all solvable problems.

like image 30
John Rayner Avatar answered Oct 10 '22 13:10

John Rayner


I kind of think of temp data as being a fire-and-forget mechanism for notifying the user. Its great to give them a reminder of something they recently did, but I'd also be hesitant to make it a required step in some user process. The reason being if they refresh the page, I believe it would be gone. Well I guess I'm also hesitant to use it as its not really well defined how reliable it is.

I wonder if the problem is that you're having the action redirect to another page before the confirm step. I wonder if instead after they first submit, you could do enough processing to generate the confirm dialog, then return the original page with the confirm question. Similar to how you might do validation, except the validation rule checks whether the confirmation step was performed (with the confirmation UI hidden until other validation passes).

like image 26
Frank Schwieterman Avatar answered Oct 10 '22 11:10

Frank Schwieterman