开发者

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?

开发者 https://www.devze.com 2023-04-06 13:00 出处:网络
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 t

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 t开发者_如何转开发he 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号