John Ng
John Ng

Reputation: 889

Exporting table in view to excel

I have a SearchModel that contains the search parameter and the search results as well. This is passed from my SearchController to my view. In the view, the search parameters are populated in the search form and the search result is shown as a table.

In the view, I have a link that should generate an excel file that contains the table. At first, I was trying to pass the model back to the SearchController using a different action name. However, the passed data (SearchModel) is null.

After searching around, it seems like I shouldn't be passing data from view to controller.

What is the proper way of doing this? I don't want to query the database again and generate the search result. The data has been generated in the Index action passed onto the view. Is there a way to retrieve this back?

Upvotes: 0

Views: 46

Answers (1)

Mike Brind
Mike Brind

Reputation: 30110

The best and "proper" way is to query the database again. Once the original View has been generated, all the components that worked together to generate it (controller, repositories etc ) have died. They no longer exist. You could save the data in memory on the server, but that is not generally recommended unless the same data is likely to be re-used frequently. And presumably, not all users want to export the data, so you will be storing data largely for no reason at all.

However, if it is likely to be reused, you can use caching. http://www.asp.net/web-pages/tutorials/performance-and-traffic/15-caching-to-improve-the-performance-of-your-website

Upvotes: 1

Related Questions