Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
MVC, WebAPI Controllers and LocalStorage
Message
From
04/07/2014 09:16:30
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 4.0
OS:
Windows 8
Network:
Novell 6.x
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01603183
Message ID:
01603233
Views:
75
>>Thanks Viv.
>>
>>I have my data stored in local storage and now I want to display it. I've been using knockout like this:
>>
>>
this.runSheetDetails = ko.observableArray(@Html.Raw(Model.RunSheetDetailsJSON));
>>
>>So I've tried changing that like this:
>>
>>
this.runSheetDetails = ko.observableArray(JSON.parse(localStorage[this.localKey]));
>>
>>but then I get binding errors:
>>
>>Uncaught ReferenceError: Unable to process binding "template: function (){return { name:'list-template',foreach:runSheetDetails} }"
>>
>>Do you have an example of reading a list of data from localstorage and displaying it?
>>
>>This is an example of what the json looks like when it comes out of local storage:
>>
>>[[{"airWayBill":{"customer":{"cus_PK":"5df642b5-7889-419b-8a16-a63196f79571","cus_lastname":"Bachan","cus_firstname":"Kathy-Ann","cus_company":"Kath-Ann Bachan","cus_addr1":"23 Alexandra Street","cus_addr2":"C/O British Academy","cus_number":"6695 "},"awb_pk":"b9311877-eaf1-49b3-bfaf-2e8afedce389","awb_pieces":1,"awb_cusfk":"5df642b5-7889-419b-8a16-a63196f79571","awb_number":"99800640310"},"deliveryHistories":[],"runSheetHeader":null,"rnd_pk":"7c8d9f45-813b-4120-945d-91e4cfbaedba","rnd_rnhfk":"548a5288-c65d-43ac-be7c-fec662fc154a","rnd_awbfk":"b9311877-eaf1-49b3-bfaf-2e8afedce389"},{"airWayBill":{"customer":{"cus_PK":"4b0a83af-ef75-4567-b564-d4288ed193a0","cus_lastname":"Ambrose","cus_firstname":"Leonardo","cus_company":"Leonardo Ambrose","cus_addr1":"11 Braemar Rd,","cus_addr2":null,"cus_number":"1463 "},"awb_pk":"bea8497f-b956-4a4a-9702-c5eddcf014c7","awb_pieces":1,"awb_cusfk":"4b0a83af-ef75-4567-b564-d4288ed193a0","awb_number":"99802580636"},"deliveryHistories":[],"runSheetHeader":null,"rnd_pk":"39d225f2-b064-45fa-8034-7d6a92aa7b4e","rnd_rnhfk":"548a5288-c65d-43ac-be7c-fec662fc154a","rnd_awbfk":"bea8497f-b956-4a4a-9702-c5eddcf014c7"}"]
>>
>>(note I deleted a lot of the data so may not ave terminated the json properly)
>
>What code did you use to put the above *into* localStorage. Can you compare the output from the MVC Html.Raw() with the above object ?

This is the code in my cshtml that saves the data into LocalStorage:
            var vm = new ViewModel();

            var serialized = JSON.stringify(vm.runSheetDetails());

            runsheetdetailsDataService.saveLocal(serialized);
This is the saveLocal code:
            saveLocal: function (newrunsheetdetail) {
                if (Modernizr.localstorage) {
                    var
                        ls = window.localStorage,
                        key = ds.localKey;

                    if (ls.getItem(key) == null) {
                        ls.setItem(key, '[]');
                    }

                    var existing = ls.getItem(key);
                    existing = ds.parse(existing);

                    existing.push(newrunsheetdetail)

                    var serialized = JSON.stringify(existing);
                    ls.setItem(key, serialized);
                }
            },
The data going in and coming back looks exactly the same.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform