4

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
ary
  • 597
  • 3
  • 8
  • 20

4 Answers4

3

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.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • No. I don't want to create a state but want to persist data between requests (Not from Controller to View). Say want to know which field a user clicked previously. – ary Apr 17 '12 at 22:04
  • 1
    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. – ary May 10 '12 at 01:05
3

I haven't done a lot of ASP.NET MVC 3 recently, but I think the only real way you can persist data across requests is either with session state, cookies, or by posting data to the server every request.

ViewData, ViewBag, and TempData are all effectively killed at the end of each request, whereas session state, cookies, and post data is made available at the beginning of every request based on information from the client browser.

What particular scenario are you trying to persist data for?

RTigger
  • 1,360
  • 10
  • 11
  • 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... – ary Apr 17 '12 at 22:01
  • If you want to do this permanently (i.e. even if the user leaves your page and comes back) I'd recommend a cookie, otherwise this is something you can store in the state. You'll have to serialize your sort data into a string and deserialize it on each page load in order to re-access the data. – RTigger May 02 '12 at 21:56
1

You could pass those values as query string parameters when redirecting.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You can set parameters to hidden input fields if you post the form. But if you gonna call action by HTTPGET then you can pass values by using QueryString parameters.

Yorgo
  • 2,668
  • 1
  • 16
  • 24