2

How can I pass an array of strings to a controller in asp.net mvc4?

Here is my code

jQuery:

function FnSaveAnalyses(){
    var checked = [];
    for (var i in checkedIds) {
        if (checkedIds[i]) {
            checked.push(i);
        }
    }
    alert(checked); // it shows all the records without problem

    var url = urlBiologieDemande + "Save";
    $.post(
                url,
                data = { values: checked},
                traditional= true,
                success = function (data) {
                    DataSaved();
                });
}

Controller

public ActionResult save(string[] values)
        {
            //Traitement
        }

When debugging, I get null values.

user3723637
  • 130
  • 8
Coder Passion
  • 65
  • 1
  • 11

1 Answers1

1

POST IT AS JSON array.

var checked = [];
for (var i in checkedIds) {
    if (checkedIds[i]) {
        checked.push(i);
    }
}
var url = urlBiologieDemande + "Save";
$.ajax({
    type: 'Post',
    dataType: 'json',
    url: url ,
    data: JSON.stringify(values:checked),
    contentType: 'application/json; charset=utf-8',
    async: false,
    success: function (data) {

    }
});

then get the JSON in the Controller and Parse it .. see this

Community
  • 1
  • 1
Muath
  • 4,351
  • 12
  • 42
  • 69