Search code examples
javascriptc#model-view-controllerasp.net-mvc-5

How to insert view bag list of data to local storage?


as i have multiple checkboxes going to viewbag and getting list of stringfrom selected check boxes. but how can i store them to localstorage? for using to make my filters checked again after submit

was trying some way but couldn't achieve any help here..?

var cc = @Html.Raw(Json.Encode(ViewBag.Courses));
       console.log("cc",cc)
localStorage.setItem("cc",cc.checked);
checked = JSON.parse(localStorage.getItem("#cc"));
   document.getElementById("#cc").checked = checked;


Solution

  • Not sure if you can? But hope this might help you.

    var cc = '@ViewBag.Courses';
    localStorage.setItem("cc",cc);
    checked = localStorage.getItem("cc");
    document.getElementById("#cc").checked = checked;