Click here to Skip to main content
15,882,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working in mvc 5 entity framework SO im having 3 filters in index view with foreach loop getting items from database and displaying through viewbags to checkbox. After selecting check box and submitting i couldn't keep the selected items still selected. Any help here..!

What I have tried:

index view code
 @foreach (var item in ViewBag.Courses){
 <input type="checkbox" id="check1" name="Course" value="@item" data-myvalue = "@ViewBag.Courses" />
<label style="font-weight:normal" for="course">  @item</label>}

      

@foreach (var item in ViewBag.Gender)
{ <input type="checkbox" id="check2" name="Gender" value="@item" data-myvalue = "@ViewBag.Gender" />
<label style="font-weight:normal" for="Gender">@item</label>
}



   @foreach (var item in ViewBag.Levels)
{<input type="checkbox" id="check3" name="Level" value="@item" data-myvalue = "@ViewBag.Levels" />
<label style="font-weight:normal" for="Level">@item</label>
}

 <input id="postBtn" type="submit" value="Submit" class="button button2">



<script>
    $('#postBtn').on('click', function () {
        debugger;

        var checkbox = document.getElementById("#check1");
        localStorage.setItem("#check1", checkbox.checked);
        var checkbox = document.getElementById("#check2");
        localStorage.setItem("#check2", checkbox.checked);
        var checkbox = document.getElementById("#check3");
        localStorage.setItem("#check3", checkbox.checked);



        $('form#form-submit').submit();

    });


    var checked = JSON.parse(localStorage.getItem("#check1"));
    document.getElementById("#check1").checked = checked;

</script>

controller
[HttpPost]
public ActionResult CheckBox(string[] Course, int? page, string[] Gender, string[] Level,string searchString)
        {
            var students = from s in cd.tblStudents
                           select s;
            if (Course != null)

            {
                students = students.Where(x => Course.Contains(x.Course));
               
            }
            if (Gender != null)
            {
                students = students.Where(x => Gender.Contains(x.Gender));
            }
            if (Level != null)
            {
                students = students.Where(x => Level.Contains(x.Level));
            }
            if (!string.IsNullOrEmpty(searchString))
            {
                students = students.Where(x => x.FirstName.StartsWith(searchString));
            }

          
            ViewBag.Courses = students.Select(x => x.Course).Distinct();


            ViewBag.Gender = students.Select(g => g.Gender).Distinct();

            ViewBag.Levels = students.Select(g => g.Level).Distinct();
            
            return View("index", students.ToList().ToPagedList(page ?? 1, 8));
        }

so as my viewbag is getting distinct iteself after submit filter i cant insert those into localstorage

so how can i get my selected checkbox persistent/keeping checked after submit
Posted
Updated 7-Dec-22 6:04am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900