1

I have a small problem with a Ajax API request. I implemented a Controller like this, which takes ajax requests from Bootgrid:

public JsonResult PostThings(int current, int rowCount, string searchPhrase, KeyValuePair<string, string> sort )

The frist three parameters are bound but "sort" is not. The problem is, that the parameter is dynamic, which means that the field name is included in brakets. It is in the form of sort[field_name]=asc.

I thought KeyValuePair<string,string> could work, but it does not just as string[] sort or just string sort.

Is there a way to bind this parameter? I also have no idea how to write a custom model binder for that purpose.

Thanks for any hints.

Marco

comar
  • 43
  • 6
  • See this http://stackoverflow.com/questions/16553561/passing-list-of-keyvaluepair-or-idictionary-to-web-api-controller-from-javascrip – 111 Mar 10 '16 at 09:14
  • Thank you so much, this was a great hint. Binding to ``Dictionary`` works fine. I used ``public JsonResult PostThings(int current, int rowCount, string searchPhrase, Dictionary sort )`` in my controller now. – comar Mar 10 '16 at 10:17

1 Answers1

1

I found the answer. Binding to Dictionary<string, string> works fine.

I used

[HttpPost]
public JsonResult PostThings(int current, int rowCount, string searchPhrase, Dictionary<string,string> sort ) 
{
    //do something
    return null;
}

in my controller now and received the values provided with dynamic parameters.

comar
  • 43
  • 6