ary
ary

Reputation: 617

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.

Upvotes: 4

Views: 5330

Answers (4)

Yorgo
Yorgo

Reputation: 2678

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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

Upvotes: 1

RTigger
RTigger

Reputation: 1368

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?

Upvotes: 3

Shyju
Shyju

Reputation: 218702

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.

Upvotes: 3

Related Questions