1

I have a variable that is a string[] parameter. I've tried to push values in the actionresult and have yet to match the string[] parameter. So my ajax:

    var panels = [];
    $('input.checkone').each(function (e) {
        if ($(this).is(':checked')) {
            var inputCheck = $(this).closest('tr').find('td:nth-child(6) a').text();
            panels.push(inputCheck);
        }
    });
    var data = { panels: panels };
    $.ajax({
      url: "/main/sub",
      data: data,
    }).etc...

So panels looks like panels: Array[1] 0:'test', and it goes to my controller:

public ActionResult sub(string[] panels)
{
 //content 
}

Currently the panels is going through like {string[1]}. I'm not sure why my strings aren't being passed in correctly and its being passed like {string[1]}.

7
  • what about using innerHTML instead of text? Commented May 1, 2017 at 18:39
  • it seems like its passing ok in the jQuery, it gets the text, but when I pass it to my controller it gets replaced with {string[1]} Commented May 1, 2017 at 18:41
  • okay what about var data = { 'panels': panels }; Commented May 1, 2017 at 18:43
  • yeah that did the exact same thing Commented May 1, 2017 at 18:45
  • Did you try adding contentType: "application/json"? Commented May 1, 2017 at 18:46

2 Answers 2

1

This code works for me:

        var url = 'http://my-address/';
        var array = ["hello", "world"];
        $.post(url, $.param({ data: array }, true), function (result) {
            $('#MyDiv').html(result);                
        }); 



public ActionResult sub(List<string> data)
{
 //content 
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to tell jQuery to serialize your parameters in the traditional way, which is not the default anymore since jQuery 3.0.

$.ajax({
    url: "/main/sub",
    data: data,
    traditional: true
})

Comments

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.