I am looking for some suggestions or possibly a solution I’ve overlooked for an issue I have with a project I am working on. It involves persisting ViewModel data between several requests. Here’s an example:
User is on a specific page that contains a few form fields and a grid of items related to the underlying model. The user needs to add an item or items to the grid, so I have to redirect them to another page to select some of these items. After the user has selected their items on this page, I need to send them back to the original page with the items displayed in the grid.
I have this working, but what I’m doing is before they are redirected to the second page, I am storing the ViewModel for the original page in session, updating the ViewModel appropriately with the selected items on the second page, then redirecting back to the original page and using the ViewModel from session to populate the form fields and grid.
However, I don’t think this is the best solution and scalability is going to be an issue since this project needs to be scalable, and throwing stuff around in session is not a desired solution. I’ve read the multitude of suggested similar questions but I’ve found nothing that really pertains to my specific situation.
Has anyone ever done anything like this and has found more reliable solutions, or possibly has some suggestions on a different implementation. Thanks.
This comes down to a basic web request limitation and every request being essentially “starting again”. Session is definitely built to allow you a place to store things beyond the request model and is your only option UNLESS you want to take on a much more advanced approach using javascript.
Checkout this post as i have already covered off the primary solutions for editing a list and some of that applies here.
In your case I would definitely recommend using a modal/dialogue popup for selecting your “other” items as this will remove most of the hassles you’ll encounter by navigating away from the page completely (and if that page is used in other places, you could easily re-use partial views to make it work in a dialogue as well as a page without duplication).
This will involve a little javascript, but will be much easier to manage as the form will actually stay present on the page and in most cases I’ve encountered is a better user experience.
Alternatively you’d need to use hidden inputs across the other pages and post your data around through the selection process. Can’t see many ways that this won’t get ugly.
In conclusion, using session is the only option if you don’t want to make a client side solution. I’d recommend the client solution as jquery has made our lives much easier for these scenarios, but it is more complicated.
PS If you do decide the dialogue is not the best option and stick with session, don’t panic about it’s scalability as there are ways you can make it more scalable by using other session storage methods like state server or even a dedicated SQL db for it.