3

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 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

That's fine, there are various work around of this but I'm looking for the reason why this problem occurring at my code. I might be missing something.
@SundarSingh How about and upvote?. I did answer your question "Can you please suggest a work around."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.