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.

5
  • try to change your data just on data = checked (without values property) Commented Nov 18, 2013 at 14:31
  • the same problem always null :( Commented Nov 18, 2013 at 14:33
  • can you check exact Request body which has been sent to server? You may use Fiddler or Firefox with Firebug installed for this. Commented Nov 18, 2013 at 14:36
  • teh post request is undefined Commented Nov 18, 2013 at 14:39
  • Hm... let's do next call: $.post(url, { 'values[]': checked }, function (data) { DataSaved(); }); Commented Nov 18, 2013 at 14:51

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.