Filter records in database according to the selected checkboxes using MVC

Sumudu De Zoysa

I have a Ajax.BeginForm in my razor view. I want to have 3 check boxes.

  1. Begineer
  2. Intemidiate
  3. advance

checkoxes can select for any combination. When I clicked submit button bellow method in my controller will triggered.

public PartialViewResult SearchCourseCriteria(){

        var courses = from s in db.CourseCategories
                      select s;
        return PartialView("_Courses", courses);

}

This is my view

 @using (Ajax.BeginForm("SearchCourseCriteria", new AjaxOptions
    {
       UpdateTargetId = "CourseList",    
       InsertionMode = InsertionMode.Replace,  
       HttpMethod = "GET"
    }))
    {
        td>
           @Html.CheckBoxFor()                                        
        </td>

        <td>
           <input type="submit" value="Search" class="btn " />
        </td>

   }

In my model there is field called CourseLevel. I want to know How to filter courses according to the selected checkboxes. EX : If I select begineer and Intermidiate checkboex. I want to get all courseCategories from that levels. I dont know how to get that result. Help please.

user3559349

In you view, generate 3 checkboxes for each value

<label>
  <input type="checkbox" name="courselevel" value="Begineer" /> // Beginner?
  <span>Begineer</span>
<label>
<label>
  <input type="checkbox" name="courselevel" value="Intemidiate" /> // Intermediate?
  <span>Intemidiate</span>
<label>
... // ditto for advance

Then add a parameter to the method

public PartialViewResult SearchCourseCriteria(string[] CourseLevel)

The value of CourseLevel will be an array of the selected checkboxes, for example [ "Begineer", "advance" ] if you checked the first and third checkboxes

You can then modify you query to

var courses  = from s in db.CourseCategories
               where CourseLevel.Contains(s.CourseLevel)
               select s;

or

var courses= db.CourseCategories.Where(c => CourseLevel.Contains(c.CourseLevel));

Side note: I would recommend you use an enum to define the values for CourseLevel

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Listing database values according to the selected filter in dropdown

From Dev

Choosing specific DBContext according to database selected in multi-database MVC application

From Dev

Checkbox to be selected according to database value

From Dev

Codeginiter populate checkboxes according to database row count

From Dev

Deleting database records MVC

From Dev

How do I insert multiple records into a mysql database, using a combination of checkboxes and html select

From Dev

Filter Array Using Checkboxes with AngularJS

From Dev

Filter Array Using Checkboxes with AngularJS

From Dev

Filter Json Using Checkboxes with AngularJS

From Dev

dropdown menu according to selected value in database

From Dev

Trying to fetch database table according to selected option

From Dev

Searching through a database using checkboxes

From Dev

Optional columns to filter dataBase records

From Dev

mvc filter by dropdown selected text

From Dev

What is the best way to filter a list of database records that can only be filtered through javascript in ASP.NET MVC

From Dev

Getting id of checkboxes selected using jquery

From Dev

How to print only selected checkboxes using javascript?

From Dev

Macro to combine selected workseets using checkboxes

From Dev

display list of selected checkboxes using javascript

From Dev

Show all records when a filter option is not selected

From Dev

Show all records when a filter option is not selected

From Dev

Filter Database values according to the login user

From Dev

Retrieving selected records from MS access database using SQL query in vb.net

From Dev

How to toggle dom elements according to the states of checkboxes using jQuery?

From Dev

Using pipes in Angular 2 to filter on checkboxes

From Dev

How to filter datagridview using multiple checkboxes

From Dev

How to filter checkboxes using JQuery 2.1.4?

From Dev

Using pipes in Angular 2 to filter on checkboxes

From Dev

Filter data according to a selected value, sorting and counted by date in Google Sheets

Related Related

  1. 1

    Listing database values according to the selected filter in dropdown

  2. 2

    Choosing specific DBContext according to database selected in multi-database MVC application

  3. 3

    Checkbox to be selected according to database value

  4. 4

    Codeginiter populate checkboxes according to database row count

  5. 5

    Deleting database records MVC

  6. 6

    How do I insert multiple records into a mysql database, using a combination of checkboxes and html select

  7. 7

    Filter Array Using Checkboxes with AngularJS

  8. 8

    Filter Array Using Checkboxes with AngularJS

  9. 9

    Filter Json Using Checkboxes with AngularJS

  10. 10

    dropdown menu according to selected value in database

  11. 11

    Trying to fetch database table according to selected option

  12. 12

    Searching through a database using checkboxes

  13. 13

    Optional columns to filter dataBase records

  14. 14

    mvc filter by dropdown selected text

  15. 15

    What is the best way to filter a list of database records that can only be filtered through javascript in ASP.NET MVC

  16. 16

    Getting id of checkboxes selected using jquery

  17. 17

    How to print only selected checkboxes using javascript?

  18. 18

    Macro to combine selected workseets using checkboxes

  19. 19

    display list of selected checkboxes using javascript

  20. 20

    Show all records when a filter option is not selected

  21. 21

    Show all records when a filter option is not selected

  22. 22

    Filter Database values according to the login user

  23. 23

    Retrieving selected records from MS access database using SQL query in vb.net

  24. 24

    How to toggle dom elements according to the states of checkboxes using jQuery?

  25. 25

    Using pipes in Angular 2 to filter on checkboxes

  26. 26

    How to filter datagridview using multiple checkboxes

  27. 27

    How to filter checkboxes using JQuery 2.1.4?

  28. 28

    Using pipes in Angular 2 to filter on checkboxes

  29. 29

    Filter data according to a selected value, sorting and counted by date in Google Sheets

HotTag

Archive