2

i'm trying to get the values from the a table column, I put it in an array then send it to my controller. Something very strange happens, you'll see below :

Here is my JS code :

var result = [];

        $('td:nth-child(' + columnNbr + ')').each(function () {
            var t = $(this).html();
            result.push(t);
        });

        $.ajax({
            type: "POST",
            url: '@Url.Action("UpdateTable", "Home")',
            dataType: "html",
            traditional: true,
            data: { values: result },
            success: function (data) {
            }
        });

My Array "result" is well populated with the values I need.

Here is my controller:

public ActionResult UpdateTable(List<string> values)
{
    return View("index");
}

This returns a 500 internal error. What's driving me crazy is this -> if I modify my JS from what I posted to this:

        //$('td:nth-child(' + columnNbr + ')').each(function () {
        //    var t = $(this).html();
        //    result.push(t);
        //});

        result.push("test1");
        result.push("test2");

The Ajax call works fine ... I checked the "typeof $.(this).html()" and all are strings!

I have no idea what's wrong.

4
  • console.log your result array, could be the size of the array been posted. Can you provide some of the test table data? Commented Jun 16, 2015 at 9:23
  • Here is my array : ["2015-06-16", "0", "0", "206000", "206000", "100", "0", "30558", "14,83", "14,83", "9", "19", "147", "0", "0", "88,04", "0", "0", "0", "0", "0", "0", "0", "0", "0", "87917", "-174422", "2849", "0", "0", "62,1", "<input type="button" value="Edit" id="4"> "]
    – Gun
    Commented Jun 16, 2015 at 9:27
  • The last item in the array is the problem ("<input type="button" value="Edit" id="4"> "). Exclude it
    – user3559349
    Commented Jun 16, 2015 at 9:30
  • Instead of { values: result } can you try - JSON.stringify({ values: result }), it worked for me that way.
    – ramiramilu
    Commented Jun 16, 2015 at 9:31

2 Answers 2

2

You just need to change var t = $(this).html(); to var t = $(this).text();

0
1

Why don't just use $.post()

var result = [];

$('td:nth-child('+columnNbr+')').each(function(){
    result.push($(this).text());
});

$.post('@Url.Action("UpdateTable", "Home")', {values: result});

Or if you need a callback:

$.post('@Url.Action("UpdateTable", "Home")', {values: result}, function(data){
    // what to do
});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.