User123
User123

Reputation: 599

ASP.NET MVC - Maintaining state of a web page

We had a silverlight application that maintained state when clicked on links and come back to it.

I was wondering if there's a way to implement something like that using Asp.net MVC? Basically right now user goes to a search page using a link in the banner, in the search page we display some items.. user clicks on one of them and another page opens up taking him to the main page that list that items information. From there the user can again click on search but this time of course a new search window opens up.

Am wondering if there is a way to load existing content form the already opened window into the new search window?

If it makes any difference the search page is ajax enabled.

Upvotes: 2

Views: 1131

Answers (3)

Minhajuddin Khaja
Minhajuddin Khaja

Reputation: 121

As we all know, HTTP is a stateless protocol, each HTTP request does not know about the previous request.

ASP.NET also provides state management techniques that can help us to maintain data when redirecting from one page to another. There are several ways.

  1. Hidden Fields (is used to hide your data on client side. It is not directly visible to the user on UI but we can see the value in the page source).

  2. Cookies (are used for storing the data but that data should be small. It is like a small text file where we can store our data, This are stored on client side memory in the browser).

  3. Query String (generally used to pass value from one page to the next).

  4. View Data (helps us to maintain the data when sending the data from controller to view. It is the dictionary of objects derived form ViewDictionary).

  5. View Bag (same as View data, except the only difference is that view bag is the object of dynamic property).

  6. Temp Data (is also a dictionary object as ViewData and stores value in key/value pair. It is derived from TempDataDictionary. It is mainly used to transfer the data from one controller to another controller).

Upvotes: 1

Sparrow
Sparrow

Reputation: 2583

As Shyju has pointed out, Http is stateless. There are several ways to store and share data between multiple pages in web applications. Just to name a few, you can use:

  • Cookies (do not save security sensitive data such as passwords in cookies)
  • Sessions
  • Browser's local storage (http://www.w3schools.com/html/html5_webstorage.asp)
  • In MVC, you can use ViewBag, ViewData or TempData
  • You can pass data as query parameters in URL

Upvotes: 4

Win
Win

Reputation: 62300

You do not want to maintain the state in ASP.Net MVC. It is a bad practice.

If you want to pass state between action methods, you can use TempData.

It uses Session State under the hood, and clear it automatically right after you retrieve the data.

ASP.Net offers some addition methods in addition to TempData. You can read more here. In your scenario, TempData is a best choice.

Upvotes: 2

Related Questions