Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store data between requests in ASP.NET MVC?

My application reads barcodes from a USB scanner. I treat them as tokens and then build commands from those tokens. There may be a situation that I need 2 or more reads before I can create a command. I need to store previous reads in some way. How can I keep some data between request? I do not to use that tokens in my view.

Only results of commands may be displayed in the view.

like image 564
kamilwydrzycki Avatar asked Mar 11 '13 11:03

kamilwydrzycki


People also ask

How do you persist data between consecutive requests in MVC?

TempData is used to pass data from current request to subsequent request (i.e., redirecting from one page to another). Its life is too short and lies only till the target view is fully loaded. But you can persist data in TempData by calling the method Keep () .

How pass data between action methods in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form. This article talks about all of these ways, and illustrates them with code examples.

Is there any way to keep or preserve TempData value to multiple requests?

You may fetch TempData in your view and store in a variable. If you set a TempData in your action method and if you read it in your view and call the "Keep" method then TempData will be persisted and will be available in next request. We can save TempData value using keep method after reading value from TempData.


1 Answers

The TempData dictionary is ideal for storing data between controller actions. It's most commonly used in the Post/Redirect/Get pattern but could apply here.

This is a good article to read about it: http://www.devcurry.com/2012/05/what-is-aspnet-mvc-tempdata.html

like image 143
Alistair Findlay Avatar answered Sep 20 '22 04:09

Alistair Findlay