Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assume that the order I send values over POST will be model bound to an array in the same order in Asp.net MVC?

Tags:

c#

asp.net-mvc

I'm am working on having a screen that allows the user to change the order of items, and when the user clicks on the "Save" button my javascript will look at the order of <li> items, and in the order it finds them it will form an array with an id value and send it back to my application. I expect the POST data to look like:

stepIds=5&stepIds=2&stepIds=1

This means that the steps are in the order of #5, then #2, and lastly #1. In my Asp.Net MVC application I plan to catch it with:

public virtual ActionResult Reorder(short[] stepIds) { }

My question is, will Asp.net MVC ALWAYS form the stepIds array in the same order that values are specified in the POST string, or do I need to do a more complicated POST in order to ensure the order the user picks is the order the server sees?

like image 762
KallDrexx Avatar asked Sep 22 '11 13:09

KallDrexx


1 Answers

No, you cannot rely on the order. The way this is implemented in ASP.NET 4.0 it will indeed preserve the order but this might change in future versions. You may look at the HttpValueCollection.FillFromString private method with Reflector to see how it is implemented.

The only reliable way to guarantee order is this:

stepIds[0]=5&stepIds[1]=2&stepIds[2]=1
like image 117
Darin Dimitrov Avatar answered Sep 21 '22 13:09

Darin Dimitrov