HTML and CSS Reference
Make sure you select the Cookies check box and click the Clear Now button. In addition to cookies,
this also deletes localStorage data.
Passing Data from Web Storage to the Server
Unlike cookies, web storage data isn't passed between the client and server with each request. If you wish
to send web storage data to the server, you must devise a programmatic way to do so. Some options to
accomplish this task are as follows:
• Hidden form field: In this approach, you first store data in localStorage or
sessionStorage objects as usual. At the time of submitting the form, you transfer
this web storage data into a hidden form field and then submit the form. The server-
side code can then read this hidden field and process the data further.
• Ajax calls: Using this approach you make an Ajax call to the server and pass the web
storage data to the server. The server-side code then processes the data further. In a
Web Forms application, the Ajax call can be made to a web method, a web service,
or a Windows Communication Foundation (WCF) service. In an ASP.NET MVC
application, the Ajax call can be made to an action method.
In the example discussed in the next section, you use jQuery to call controller action methods.
An Example of Using Local Storage in a Survey Form
This section presents a more realistic example than the previous one that uses all the information about
localStorage discussed so far. You develop a simple survey form that captures user feedback. As you may
be aware, filling out a survey may not be a priority for end users while they're using your web site. They
may begin completing the survey form and then navigate to some other part of the web site that interests
them more. They may even close the browser and come back to your web site later. In such scenarios, it
would be nice to persist the survey data locally as the user is entering it. Later, when the user comes
back, you can reload the persisted data and save the user time. The survey application is developed using
ASP.NET MVC, and its main view is shown in Figure 7-6.