Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard way to persist data between requests in ASP.NET-MVC

What is the most standard or best way to persist data between requests?

Should I use cookies or session variables? I'm interested in keeping data like sort order, sort column, and page number (for paginiation).

I'm coming from a webforms background so normally this type of thing was automatically handled for me in the viewstate of the controls I was using.

update

I like the querystring idea, for searching and more meaningful URLs; however, I'm working on an "index/list" view, which consists of a View with header, and "control" options, like DDLs for filtering and a partial view that renders the table of data.

The DDLs use a $.load() to call an ActionResult on the controller, which returns the partial view, passing parameters there in the querystring, but since these are ajax requests the main page url of the user's browser does not get updated.

Is there a best-practice for taking querystrings off the main-page URL and using them in ajax requests to other ActionResults?

like image 766
Nate Avatar asked Mar 18 '10 16:03

Nate


People also ask

How do you persist data between consecutive requests in MVC?

TempData is used to pass data from current request to subsequent request (i.e., redirecting from one page to another). Its life is too short and lies only till the target view is fully loaded. But you can persist data in TempData by calling the method Keep () .

What is TempData keep in MVC?

TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.

Why we use AJAX call in MVC?

It is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh. The Ajax speeds up response time. In other words, Ajax is the method of exchanging data with a server, and updating parts of a web page, without reloading the entire page.


2 Answers

If you want it to survive only through one request/redirect TempData is your friend.

However, for things like your pagination, URL is the best method, for the ability to share links alone.

like image 110
Chad Ruppert Avatar answered Sep 24 '22 13:09

Chad Ruppert


A standard way is to pass those sort of things via URL Query Parameters. You can modify your routing to expect certain URL variables. That way the pages become more search engine friendly as well.

like image 24
Turnkey Avatar answered Sep 24 '22 13:09

Turnkey