Click here to Skip to main content
15,942,156 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is the controller code i was trying for 2 different filters.!

methods for filters mvc these filters are for getting gender of a student and also level he is studying in. Code below can get only 1 filter at a time from a table students .

Here is the controller code i was trying for 2 different filters.!


How to apply multiple filters at a single time from the above code.what should i add or change?

How can i check "Female student studying a specific course" with filters


What I have tried:

index method--

public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page,string Course)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            //ViewBag.Courses = cd.tblStudents.Select(x => x.Course).ToList();

            var students = from s in cd.tblStudents
                           select s;
            ViewBag.Courses = students.Select(s => s.Course).Distinct();
            ViewBag.Gender = students.Select(g => g.Gender).Distinct();
            ViewBag.Levels = students.Select(g => g.Level).Distinct();
            if (!String.IsNullOrEmpty(searchString))
            {
                students = students.Where(s => s.FirstName.Contains(searchString)
                                       || s.Lastname.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    students = students.OrderByDescending(s => s.FirstName);
                    break;
                case "Date":
                    students = students.OrderBy(s => s.DOJ);
                    break;
                case "date_desc":
                    students = students.OrderByDescending(s => s.DOJ);
                    break;
                default:
                    students = students.OrderBy(s => s.FirstName);
                    break;
            }
            return View(students.ToList().ToPagedList(page ?? 1, 8));
        }


Filter methods---

public ActionResult GetStudentsByclass(string CourseName, int? page)
        {
            ViewBag.Courses = cd.tblStudents.Select(x => x.Course).Distinct();

            ViewBag.Gender = cd.tblStudents.Select(g => g.Gender).Distinct();

            ViewBag.Levels = cd.tblStudents.Select(g => g.Level).Distinct();
            ViewBag.CurrentFilter = CourseName;
            var prods = cd.tblStudents.Where(x => x.Course == CourseName).ToList();
            return View("Index", prods.ToPagedList(page ?? 1, 8));
        }<pre>


public ActionResult GetStudentsByGender(string Gender, int? page)
        {
            ViewBag.Courses = cd.tblStudents.Select(x => x.Course).Distinct();

            ViewBag.Gender = cd.tblStudents.Select(g => g.Gender).Distinct();

            ViewBag.Levels = cd.tblStudents.Select(g => g.Level).Distinct();
            ViewBag.CurrentFilter = Gender;
            var prods = cd.tblStudents.Where(x => x.Gender == Gender).ToList();
            return View("Index", prods.ToPagedList(page ?? 1, 8));
        }


Index cshtml
<!-- single sidebar -->
                    <div class="aa-sidebar-widget">
                        <h3>Branches</h3>

                        <ul class="aa-catg-nav">
                            @foreach (var item in ViewBag.Courses)
                            {
                <li><a href="@Url.Action("GetStudentsByclass", new { CourseName = item ,currentFilter = ViewBag.CurrentFilter})">@item</a></li>

                        </ul>
                    </div>
                   
                    <!-- single sidebar -->
                    <div class="aa-sidebar-widget">
                        <h3>Gender</h3>
                        <ul class="aa-catg-nav">
                            @foreach (var item in ViewBag.Gender)
                            {
                <li><a href="@Url.Action("GetStudentsByGender", new { Gender = item ,currentFilter = ViewBag.CurrentFilter})">@item</a></li>}
                        </ul>
                    </div>
Posted
Comments
[no name] 15-Nov-22 12:24pm    
Somewhere in there you should be using AND's and OR's in your queries. Or chaining.

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