Unable to parse Array Type Query string to List of Object

977 views Asked by At

I am trying some paging and sorting stuffs in ASP.net core MVC 6 application. But when I pass array like query string MVC action unable to parse it to list.

Query String looks like :

take=10&skip=0&page=1&pageSize=10&sort%5B0%5D%5Bfield%5D=price&sort%5B0%5D%5Bdir%5D=asc

Model for it looks like:

enter image description here

This is the query string which i am getting at server: enter image description here

Count for Sort Array or List is always 0. enter image description here

Can you please suggest a work around. It should parse it correctly but not getting where things are wrong.

2

There are 2 answers

2
RonC On

Someone may have a better answer, but if it were me I'd simplify my query string a bit and flatten the model. So the query string might be:

take=10&skip=0&page=1&pageSize=10&sortfield1=price&sortdir1=asc&sortfield2=otherfield&sortdir2=desc

Then in the model replace public List<Sort> Sort with the flattened properties:

 public string SortField1 {get; set;}
 public string SortDir1   {get; set;}
 public string SortField2 {get; set;}
 public string SortDir2   {get; set;}

You can add as many of these flattened properties as needed. It's not elegant, but it gets the job done. Then if you need your Sort list built you can build it from these properties easily.

0
Sithira Pathirana On

Your current decoded query string looks like

take=10&skip=0&page=1&pageSize=10&sort[0][field]=price&sort[0][dir]=asc

So use following format to pass query string parameters from your client application

sort[0].field=price&sort[0].dir=asc