Search code examples
c#jqueryasp.net-mvctablednd

Send Ajax call to action but how to convert given string data to something usable


I am using tableDnD to drag and drop rows in a table, after the rows have been dropped i want an updated list of data to tell me the new order of the rows. I want this data sent back up to the action via AJAX.

this is my function and ajax call

        $(document).ready(function() {
        $('#profileTable').tableDnD({
            onDrop: function(table, row) {
                var rows1 = table.tBodies[0].rows;
                $.ajax('@(Url.Action("SaveTopTenGames"))',
                    {
                        type: 'POST',
                        cache:false,
                        data: {json:$.tableDnD.serialize()},
                        success:function(st){

                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            console.log(textStatus, errorThrown);
                        }
                    });

            }
        });
    });

and this is currently the string data im receiving in my action

"profileTable%5B%5D=1&profileTable%5B%5D=2&profileTable%5B%5D=4&profileTable%5B%5D=5&profileTable%5B%5D=3&profileTable%5B%5D=6&profileTable%5B%5D=7&profileTable%5B%5D=8&profileTable%5B%5D=9&profileTable%5B%5D=10"

Now I have no idea how to convert that string into anything I can use. Any suggestion on what I should do ?


Solution

  • If you want to replace the json string and split it into Array then try This:

    string json = "profileTable%5B%5D=1&profileTable%5B%5D=2&profileTable%5B%5D=4&profileTable%5B%5D=5&profileTable%5B%5D=3&profileTable%5B%5D=6&profileTable%5B%5D=7&profileTable%5B%5D=8&profileTable%5B%5D=9&profileTable%5B%5D=10";
    var str = json.Replace("%5B", "[").Replace("%5D", "]");
    var strArray = str.Split('&');
    
    for (int i = 0; i < strArray.Count(); i++)
    {
        //get the required value from strArray
        //Console.WriteLine(strArray[i]);
    }