Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persist data in MVC Razor without using TempData between requests

How do I can persist data in MVC Razor without using TempData between requests?

I see when we can use TempData from this, but don't want to TempData as it creates a state on the machine.

Thanks, Anish

EDIT: I want to persist the previous sort direction in a View page where a user is allowed to sort fields such as Name, Age etc.

FIX: I fixed it using ViewBag. Previous sort field/direction sent to View from Controller using ViewBag and then passed it back as query string in the next click.

Good FIX: I handled the everything in .js file such as checking and then in setting the previous sort field and previous sort dir in Controller.

This is what I finally did. I used ViewBag to send previous details to ViewPage and did the validation in a .js based on the current user action and passed it back to the controller in form-data.

like image 319
ary Avatar asked Feb 20 '23 19:02

ary


1 Answers

Maintaining State in client page is something which is breaking the concept of HTTP which is stateless. Why do you want to maintain state ? If you are looking for some solution for Passing some data from your controller action to corresponding view, i would suggest you to go with a ViewModel where you fill the data for the dropdown and send that ViewModel object to the strongly typed view. You will have your data available there. Also you should grab data from your DataLayer ( from tables/ or Cache or etc..) in every request if you want some data. You may pass a relevant id in the query string to get the corresponding data.

As RTigger mentioned, you may store some data in session. That will be available across all the pages till the life time of that session.

like image 169
Shyju Avatar answered Apr 07 '23 13:04

Shyju