1

I'm using tableDnD to re-order table rows, and then serialize them using "$.tableDnD.serialize()"

I want to send this to C# for processing, what's the best of doing so?

The below is an example of the serialization

pages[]=&pages[]=21&pages[]=23&pages[]=34&pages[]=37&pages[]=40&pages[]=43&pages[]=46&pages[]=49&pages[]=31&pages[]=50&pages[]=51&pages[]=52&pages[]=53&pages[]=54&pages[]=55&pages[]=56&pages[]=57&pages[]=58&pages[]=61&pages[]=65&pages[]=70&pages[]=74&pages[]=77&pages[]=78&pages[]=79&pages[]=82&pages[]=85&pages[]=88&pages[]=91&pages[]=94&pages[]=97&pages[]=100&pages[]=103&pages[]=106&pages[]=109&pages[]=112&pages[]=115&pages[]=119&pages[]=122&pages[]=123

Important Information I tagged this as MVC but forgot to mention it. I'm using ASP.NET MVC

3 Answers 3

2

You can send that as-is using one of jQuery's ajax methods. I would prefer to turn it into a smaller, neater CSV string before sending it to the server as follows:

var str = 'pages[]=&pages[]=21&pages[]=23&pages[]=34&pages[]=37&pages[]=40&pages[]=43&pages[]=46&pages[]=49&pages[]=31&pages[]=50&pages[]=51&pages[]=52&pages[]=53&pages[]=54&pages[]=55&pages[]=56&pages[]=57&pages[]=58&pages[]=61&pages[]=65&pages[]=70&pages[]=74&pages[]=77&pages[]=78&pages[]=79&pages[]=82&pages[]=85&pages[]=88&pages[]=91&pages[]=94&pages[]=97&pages[]=100&pages[]=103&pages[]=106&pages[]=109&pages[]=112&pages[]=115&pages[]=119&pages[]=122&pages[]=123';

var tmpArr = str.split('&');
var pagesArr = [];
for(var i = 0;i < tmpArr.length; i++) {
    var paramArr = tmpArr[i].split('=');
    if(paramArr[1] != null && paramArr[1] != '') {
        pagesArr.push(paramArr[1]);
    }
}
alert(pagesArr); //now much prettier

//turn it into a CSV string
var pagesCsv = pagesArr.join(',');

$.ajax({
   type: "POST",
   url: "some.aspx",
   data: pagesCsv,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });
Sign up to request clarification or add additional context in comments.

2 Comments

I'm simply posting it, I can receive that string in C#, what I'm wondering is if there's a way to interpret it as an array in c# like you can do in PHP like for example isocra.com/articles/ajaxTest_php.html
@Shahin - I'm not sure as I'm not a ASP.NET guy, but you can send that string to the server and then convert it into an array by splitting it at the comma ','
0

Based on the example of karim79 :

var str = 'pages[]=&pages[]=21&pages[]=23&pages[]=34&pages[]=37&pages[]=40&pages[]=43&pages[]=46&pages[]=49&pages[]=31&pages[]=50&pages[]=51&pages[]=52&pages[]=53&pages[]=54&pages[]=55&pages[]=56&pages[]=57&pages[]=58&pages[]=61&pages[]=65&pages[]=70&pages[]=74&pages[]=77&pages[]=78&pages[]=79&pages[]=82&pages[]=85&pages[]=88&pages[]=91&pages[]=94&pages[]=97&pages[]=100&pages[]=103&pages[]=106&pages[]=109&pages[]=112&pages[]=115&pages[]=119&pages[]=122&pages[]=123';

var pos = null;
var index = 0;

while ((pos=str.indexOf("[]"))>-1)
{
    str = str.substr(0, pos-1) + "_" + (index) + str.substr(pos+2);
    index++;
}

alert(str);


$.ajax({
   type: "POST",
   url: "some.aspx",
   data: str,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

In C#

        string[] keys = Request.QueryString.AllKeys;
        Array.Sort(keys);

        StringBuilder sb = new StringBuilder();
        foreach (string key in keys)
        {
            if (key.IndexOf("pages_")!=-1)
            {
                sb.Append(Request.QueryString[key]);
            }
        }

        // sb container the all values

Comments

0

You can also put everything in to one object like this:

var prm = { pages=[...], someOtherPages=[], additionalParam="", integer=1324 }
jQuery.ajax({
    url: "someurl.aspx",
    type: "POST",
    data: {prm: JSON.stringify(prm)},
    });

and parse prm on C# side by using this JSON Parser:

JObject json = JObject.Parse(Request.Form["prm"]);
JArray items = json["pages"] as JArray;
foreach (JToken item in items)
{
   int i = item["type"].Value<int>(); // take an item as int
   string s = item["type"].Value<string>(); // take an item as string
   JArray ar = item["complex"] as JArray; // that an item as an array
}

much simplier and flexible

1 Comment

I like the look of this solution, thanks I'm going to give this a go now

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.