简体   繁体   中英

how to get the integer array to pass to controller using data in ajax

This is the jquery ajax to add data from one selectlist to another. I'm getting first Id if i use parseInt but i want an array and the selectlist should be refreshed as i add(on click of #selImpactArea) even this isn't working

 //dialog popup $("#CcUninitDept").dialog({ title: "Add Departments", modal: true, resizable: false, dialogClass: 'custom-dialog', buttons: { Add: function() { var Departments = new Array(); $.each($("#selUninitDeptDialog option:selected"), function() { Departments.push($(this).val()); }); if (Departments.length > 0) { $.ajax({ type: "post", url: "/ChangeControl/AddInitiatedDepartments", data: { CcId: CcId, Departments: parseInt(Departments) }, processing: false, asynch: true, cache: false, dataType: "Json", success: function(data) { $.each(data, function(key, value) { $('#selImpactArea') .append($("<option></option>") .val(value.Department_Id) .html(value.Department_Name)); //$('#selImpactArea').find('option').reload(); }); }, error: function(response) { alert(response.responseText); } }); } }, Cancel: function() { $(this).dialog("close"); } } }); 

well, parseInt parses a value and returns an integer. So if I'm correct, you want this:

data: {
    CcId: CcId,
    Departments: parseInt(Departments)
},

to be:

data: {
    CcId: CcId,
    Departments: Departments.map(val => parseInt(val, 10)),
},

This only works if Departments is an array.

我通过添加传统的属性data: { CcId: CcId, Departments: Departments}, traditional:true,找到了解决方案data: { CcId: CcId, Departments: Departments}, traditional:true,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM