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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With