Skip to content
Advertisement

Use inputbox to filter in asp.net core razor with dapper

I have a query that is already getting data from the DB, Now I want to filter the data using a checkbox from the razor page. Below here is the checkbox

I am already getting the values of the checkboxes via string[] Requestloans But the problem is how to filer db with the RequestLoans array using dapper. If this is possible for two then I can also apply it to more than two

Advertisement

Answer

If you manage to get your input variables inside a Dictionary<string, object> before the call, either through data binding the variables or otherwise getting them when you need them. The following method can filter it for you.

This assumes you make the keys the column names. And that in this case they’re all from the same table alias (though you can edit this as needed. It’s just a way to keep building a prepared statement adding where conditions on the fly).

If you have a dictionary build up as such:

The parameterised query becomes:

Upon execute you supply the parameter values, and Dapper is pretty good at the type matching based on your values. This also works around some dangers of SQLinjection.

There are other ways as well to build up the query dynamically, or insert parameters. Getting the values into the dictionary i’ll leave to you.

Dapper explanation

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement