admin管理员组

文章数量:1208153

What am I doing wrong here?

I can successfully pass 4 bool params to a controller. Now I want to pass an array of int to my controller but it doesn't work - I've left my working code in the example(commented out) so you can see I'm not changing that much - I think I'm missing something simple (it is 17:44 afterall!!!). I can see the array is populated using the alert(rolesChecked); statement:

var rolesChecked = [];
$('[type="checkbox"].role-checkbox').each(function () {
    if (this.checked)
    {
        rolesChecked.push($(this).val());
    }
});

alert(rolesChecked);

//var administrator = $('#cbAdministrator').is(":checked");
//var manager = $('#cbManager').is(":checked");
//var technician = $('#cbTechnician').is(":checked");
//var transcriber = $('#cbTranscriber').is(":checked");

if (rolesChecked.count > 0){//administrator || manager || technician || transcriber) {
    $.ajax({
        url: '@Url.Action("GetFutureHolidays", "Employee")',
        type: 'GET',
        dataType: 'json',
        // we set cache: false because GET requests are often cached by browsers
        // IE is particularly aggressive in that respect
        cache: false,
        data: {
            roleIdXXXs: rolesChecked
            //includeAdministrator: administrator,
            //includeManager: manager,
            //includeTechnician: technician,
            //includeTranscriber: transcriber
        },
        success: function (data) {

            //do something...
        }
    });
}

Controller Action:

public string GetFutureHolidays(List<int> roleIdXXXs)//bool includeAdministrator, bool includeManager, bool includeTechnician, bool includeTranscriber)
{
    //do something
}

with the old code, the controller action would be hit... with the array, it never gets hit... What am I missing here...

also, I think List<int> roleIdXXXs should be fine, but I also tried List<string>, int[] and string[] in case it isn't!!!

What am I doing wrong here?

I can successfully pass 4 bool params to a controller. Now I want to pass an array of int to my controller but it doesn't work - I've left my working code in the example(commented out) so you can see I'm not changing that much - I think I'm missing something simple (it is 17:44 afterall!!!). I can see the array is populated using the alert(rolesChecked); statement:

var rolesChecked = [];
$('[type="checkbox"].role-checkbox').each(function () {
    if (this.checked)
    {
        rolesChecked.push($(this).val());
    }
});

alert(rolesChecked);

//var administrator = $('#cbAdministrator').is(":checked");
//var manager = $('#cbManager').is(":checked");
//var technician = $('#cbTechnician').is(":checked");
//var transcriber = $('#cbTranscriber').is(":checked");

if (rolesChecked.count > 0){//administrator || manager || technician || transcriber) {
    $.ajax({
        url: '@Url.Action("GetFutureHolidays", "Employee")',
        type: 'GET',
        dataType: 'json',
        // we set cache: false because GET requests are often cached by browsers
        // IE is particularly aggressive in that respect
        cache: false,
        data: {
            roleIdXXXs: rolesChecked
            //includeAdministrator: administrator,
            //includeManager: manager,
            //includeTechnician: technician,
            //includeTranscriber: transcriber
        },
        success: function (data) {

            //do something...
        }
    });
}

Controller Action:

public string GetFutureHolidays(List<int> roleIdXXXs)//bool includeAdministrator, bool includeManager, bool includeTechnician, bool includeTranscriber)
{
    //do something
}

with the old code, the controller action would be hit... with the array, it never gets hit... What am I missing here...

also, I think List<int> roleIdXXXs should be fine, but I also tried List<string>, int[] and string[] in case it isn't!!!

Share Improve this question asked Oct 8, 2015 at 16:45 PercyPercy 3,1154 gold badges36 silver badges61 bronze badges 1
  • Possible duplicate: stackoverflow.com/questions/15782417/… – Maria Ines Parnisari Commented Oct 8, 2015 at 16:51
Add a comment  | 

4 Answers 4

Reset to default 18

You need to add the traditional: true ajax option to post back an array to the collection

$.ajax({
    url: '@Url.Action("GetFutureHolidays", "Employee")',
    type: 'GET',
    dataType: 'json',
    cache: false,
    data: { roleIdXXXs: rolesChecked },
    traditional: true, // add this
    success: function (data) {
    }
});

Refer also the answer to this question for more detail on what the options does and the form data it generates.

In your if statement, instead of rolesChecked.count, use rolesChecked.length

You can't submit a list like this from Ajax, the quickest fix in the process you are using is to use serialization-desalinization process, you can send it as

roleIdXXXs: JSON.stringify(rolesChecked)

on Action:

public ActionResult GetFutureHolidays(string rolesChecked)
{
    var test = new JavaScriptSerializer().Deserialize<List<int>>(rolesChecked);
}

You should use JSON.stringify() on you AJAX call lice this:

data: {
            roleIdXXXs: JSON.stringify(rolesChecked)
            //includeAdministrator: administrator,
            //includeManager: manager,
            //includeTechnician: technician,
            //includeTranscriber: transcriber
        }

本文标签: passing an array of int to MVC controller using ajax javascriptStack Overflow