0

I have a requirement to create multiple quick filters on a table data in ASP.NET razor pages application.

For example filters like Show only my issues, Show issues from London location etc.

I want to manage all these quick filters using a single route parameter FilterBy which can make the url like this:

baseURL?FilterBy=OnlyMyIssues&FilterBy=LondonLocation

Can someone please help me with some way for this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Can you show the code that you use to filter, pls – Serge Aug 01 '21 at 08:47
  • @D Gogana,the code of `Noah Stahl` can work,you can try to check the [link](https://stackoverflow.com/questions/43397851/pass-array-into-asp-net-core-route-query-string) and check if there is something wrong in your code. – Yiyi You Aug 02 '21 at 06:50

1 Answers1

1

You can use a parameter of type string array (string[]) to capture multiple values of the same type and query string key. For example:

[HttpGet("/GetStuff")]
public IActionResult GetStuff([FromQuery] string[] FilterBy)
{
    // Apply each value in FilterBy, if any
}

For this query:

GET /getstuff?filterby=foo&filterby=bar

You should see this come into the API method:

enter image description here

If your filters expect different types of values, like booleans and strings, this wouldn't work or be a good design. Better to use dedicated keys like issueType and location each expecting the appropriate type of value.

Noah Stahl
  • 6,905
  • 5
  • 25
  • 36
  • Thanks for the suggestion Noah. I did try using string array but that didn’t seem to work. I will look towards your suggestion to use separate keys. Thanks – D Gogana Jul 31 '21 at 22:09
  • Please add the code you've tried to your question. – Noah Stahl Aug 01 '21 at 13:59