0

I'd like to pass a list of parameters in an action.

I would like to use Tuple, but I saw on the internet that's not possible.

I'm building a search action which will search into many fields of an object.

So I need to pass in parameter to my action, some pair of values like below :

Row0 : "Name,Chris"
Row1 : "City, NY"

The query will return every objects in my table which have name = Chris and city = NY

Here is my action :

[HttpGet("objects/searchMultiple/{type}/{operateur}/{param}", Name = "searchMultiple")]
public IActionResult SearchMultiple(string type, string operator, What_To_Put_Here?)
{
   //call of the function which will execute the query with all parameters.
}
Vincent Ducroquet
  • 864
  • 4
  • 14
  • 24
  • 1
    did you ever hear modelbinder you can create your custom query parameter type. – hasan Jun 15 '17 at 08:43
  • No, I'm beginner in Asp.Net. Thanks I will search – Vincent Ducroquet Jun 15 '17 at 08:44
  • your welcome :) you can take a look at this https://stackoverflow.com/questions/9508265/how-do-i-accept-an-array-as-an-asp-net-mvc-controller-action-parameter – hasan Jun 15 '17 at 08:45
  • Instead of trying to reinvent the wheel you might want to check out [OData](https://www.google.com/search?q=web+api+odata). Using that your action returns an IQueryable and the query is created and passed by the client in the URL. – Igor Jun 15 '17 at 08:52
  • You can use a Dictionary.. or you could create a class to hold your params. If you know on which fields you are filtering having a type makes more sense and I guess you can change the client which is sending the request, to comply with this format. But maybe I'm wrong.. – jpgrassi Jun 15 '17 at 11:50
  • If I pass a dictionnary as action parameter, could you give me an exemple of the Get Request? – Vincent Ducroquet Jun 15 '17 at 11:55
  • Can you show us how you are making the request? Is it from javascript, angular or something like that? – jpgrassi Jun 15 '17 at 11:57
  • I'm just testing with "Postman" a chrome plugin. Actually I'm wondering how I'll pass parameters in the get for the dictionnary – Vincent Ducroquet Jun 15 '17 at 11:58
  • My get is like that : http://localhost/api/objects/searchMultiple/contact/and/{dictionnary_params} Where "contact" and "and" are other parameters – Vincent Ducroquet Jun 15 '17 at 12:00
  • And to execute the request i'll use jquery – Vincent Ducroquet Jun 15 '17 at 12:03
  • So you could pass and/city="NY"&name="john", and bind that to a class with the same properties. The framework will do the mapping for you. – jpgrassi Jun 15 '17 at 12:03
  • Yes but I need multiple fields ^^ Because in my database I have dynamic objects, with dynamic column( in a JSON) So I can't guess what fields my users will decide to search in. So this is why I need to pass multiple pairs of values. Like [name,Chris], [city,NY], [age,40] – Vincent Ducroquet Jun 15 '17 at 12:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146758/discussion-between-jpgrassi-and-vincent-ducroquet). – jpgrassi Jun 15 '17 at 12:09

1 Answers1

0

I found a solution

[HttpGet("objects/searchMultiple/{type}/{operateur}", Name = "searchMultiple")]
public IActionResult SearchMultiple(string type, string operateur, Dictionary<String,String> parameters)

And the GET request

http://localhost/api/objects/searchMultiple/contact/and?parameters[mail]=gmail&parameters[name]=Chris

Then in the action in the controller, the dictionary in parameter contains

[0][mail,gmail]
[1][name,Chris]
Vincent Ducroquet
  • 864
  • 4
  • 14
  • 24