-2

I want to preserver state in MVC with js when browse page back. I tried all of the tricks below. None of the alerts appears?

all the alerts will appear if the code is placed in a page of a webform site, but none of them will appear if you place it in a page of a MVC site. In a MVC 3 page, I want to find a place to preserve checkboxes state with javascript WHEN users press the browser's "previous" button.

I can't use server side code such as How to handle checkboxes in ASP.NET MVC forms? or Maintain state of a dynamic list of checkboxes in ASP.NET MVC because we need embed existing js code

Appreciate any helps with other ideals.

<body onunload="">
<script type="text/javascript">
    alert('press previous button, reload1');

    alert('press previous button, reload2');
    history.navigationMode = 'compatible';
    $(document).ready(function () {

        //wire up checkboxes.  //$("input[name='element12']")   //($"input[name="addedFav"]") 
        //   $("input[name='addedFav']").live('change', function (e) {
        alert('press previous button, reload3');

    });

    window.onload = function () { alert('press previous button, reload4'); };

</script>
<a href="http://stackoverflow.com">click me, then press the back button</a>

</body>
Community
  • 1
  • 1
joejh
  • 7
  • 2
  • what behaviour are you seeing? what are you expecting? – Darko Jun 01 '12 at 13:44
  • Do you see any errors in the Javascript console? – jrummell Jun 01 '12 at 13:44
  • I want to add logic with JavaScript to keep the state of checkboxes when user browses back to the page by pressing "previous" button. But it looks like I got no place to add those JavaScript – joejh Jun 01 '12 at 13:47
  • no errors at all. I just want to see any of those alerts appears so that I know where to add logic to keep the state of the checkboxes, this has been a problem with MVC since it doesn't keep any viewstate – joejh Jun 01 '12 at 13:51
  • This doesn't appear to have anything to do with asp.net-mvc (which doesn't even use viewstate as a tag you added), because your code shows javascript. – Erik Philips Jun 01 '12 at 13:53
  • All the alerts will appear if the code is in a webform page, but none of them will appear if you place it in the page of MVC site – joejh Jun 01 '12 at 13:56

1 Answers1

0

It seems like you might have another javascript code block on your page that is throwing an unhandled error. Try checking your browser console for the exact line

..edit.. If all you want to do is store checkbox state, you should try using store.js. You can attach jquery click handlers to the checkboxes that serialize the selected checkboxes and store them. Then on page load you check to see if the store is populated, if so deserialize and select the appropriate checboxes

Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50
  • Thanks Jason, all the four alters appear when the page is loaded for the first time. But none of them appears when you click on the link "click me, then press the back button" and then press back button to go back to this page (in a MVC3 site:) while of course all of them appear in that case if it is placed in a webform site. I don't have any other javascript included. I know this happens because MVC doesn't implement viewstate, just wonder whether there is any workaround for the case when user uses back button, it looks like that it got ingored by MVC, here I can't use server side code – joejh Jun 01 '12 at 14:31
  • (such as http://stackoverflow.com/questions/3291501/maintain-state-of-a-dynamic-list-of-checkboxes-in-asp-net-mvc/3298821#3298821) because we need embed existing js code) – joejh Jun 01 '12 at 14:32
  • Thanks Jason, the store.js is very interesting. But the problem here is that the onunload, windows.onload (therefor checking on page load won't work), or document.ready won't got triggered in MVC3 WHEN the page is viewed by pressing the back button. Where should I place code to "check to see if the store is populated"? – joejh Jun 01 '12 at 14:59
  • Inside your dom ready function. $(function(){ //check for stored data here}) – Jason Kulatunga Jun 01 '12 at 15:44
  • As I mentioned, one of the problems is the alert('press previous button, reload3') won't be triggered in MVC WHEN browsing page back to this page. I am looking into some js frameworks such as store.js or backbone.js – joejh Jun 01 '12 at 15:59