2

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.

1 Answer 1

2

The above is the answer. Hope this is useful to others.

var order = Request.Params["id[]"];
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure it is not rather Request.Params["id"] ? I am just getting null with Request.Params["id[]"]. However, I am getting a string with Request.Params["id"].

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.