3

I have the following scenario that may warrant storing data in a conroller member variable in order to share it between actions.

I have a search form and a button - when clicked, a table full of data returns according to the search form parameters. One action - all good and clean.

I am now asked to put an excel button so that the user can download the table in excel format. I don't want to run the db query again, since the data is already there, but since I am using a server side Excel component, I need the data to be available on the server in order to shove it into Excel.

My initial idea was to have an extra variable in my controller where the data can be stored. But I have never seen this being done in asp.net MVC. Is this an accepted pattern? My understanding was that each action is sort of isolated.

sarsnake
  • 26,667
  • 58
  • 180
  • 286

3 Answers3

2

The controller is thrown away after each request. You get a new one everytime. If you want to store the data, the easiest would be to put it into session state.

Andy
  • 8,432
  • 6
  • 38
  • 76
2

The MVC pattern encourages statelessness. This means if you want to keep data between actions it needs to get posted from the client (the server shouldn't maintain any state relating to a specific session). (so yes each action should be isolated, and shouldn't be reliant on any previous action, ie it shouldn't rely on setup from a previous request)

Take a read of scott Gu's blog on what MVC means here http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx

If you are having performance problems accessing data my recommendation is to use serverside caching of your data fetch. This should ideally be implemented in your data access or query layer in your application and shouldnt be strongly tied to a specific session

undefined
  • 33,537
  • 22
  • 129
  • 198
  • thanks for the post. I sort of intuitively knew that and it makes perfect sense - consider the web is stateless. – sarsnake Jan 28 '12 at 01:28
1

I would suggest to use TempData, for example it's described here http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

Update: return file as done here Returning a file to View/Download in ASP.NET MVC

It's a normal practice to have a separate controller to return the file.

Community
  • 1
  • 1
Kath
  • 1,834
  • 15
  • 17
  • Which limit are you talking about? – Kath Jan 28 '12 at 02:00
  • I just read the article and it looks like TempData is not a good fit here, as I am not going to redirect anywhere. The user may or may not press Excel button and I can't predict when they would. – sarsnake Jan 28 '12 at 02:11
  • Ok, I've got. You just want to return exel file. It's not a problem. See update. – Kath Jan 28 '12 at 02:56
  • Thanks Kate. Returning the file is not the question. The issue is the data that goes in the file. I generate the file based on the data displayed. So my question was more to do with "how not to repeat fetching the data part since it's already on the page". Looks like I will have to as there is no feasible way to store that much data inside TempData. – sarsnake Jan 30 '12 at 17:25