This is one of those "I figured it out but it was painful so I'm posting this to help others" questions.
I'm building a jQuery based app on ASP.NET Razor. I am using jQuery.ui sortable to enable sorting of things.
It was non-obvious to me how to pass the results of a sortable event to my Razor page. There are lots of examples for PHP, but I couldn't find anything for Razor.
Here's an example jQuery.ui for sortable:
$('#Categories').sortable({
update: function () {
var catOrder = $(this).sortable("serialize").toString();
$.ajax({
type: "POST",
url: "OrderCategories",
data: catOrder,
}).done(function (msg) {
alert('done: ' + msg);
});
}
});
This passes a string that looks like this to the OrderCategories page:
{id[]=2&id[]=3&id[]=1&id[]=4&id[]=5}
Apparently ASP.NET is smart enough to figure out a query string like this is an array. All you have to do to get this array is
var order = Request.Params["id[]"];
Now order is an array of integers representing the order of the list. Took me way too long to figure this out. Hope this helps.