How to send Array with the formdata in ajax request to mvc action

。_饼干妹妹 提交于 2019-12-04 08:02:12

FormData is a set of name/value pairs representing form controls and their values, and is sent as multipart/form-data, You cannot stringify() it and/or send it with separate objects. You need to append name/value pairs to it.

If the element with id="supportedLanguages" is inside the form with id="frmAppSettings", then your code

var formdata = new FormData($('#frmAppSettings').get(0));

will correctly add the values of the <select> to the FormData object. If not, then you need to append each value in the array, for example

var formdata = new FormData($('#frmAppSettings').get(0));
$.each($("#supportedLanguages").val(), function(index, item) {
    formdata .append('SelectedLangs', item);
});

and then the ajax options need to be

$.ajax({
    type: "POST",
    url: '@Url.Action("AppSettings", "AppMenuMaker")', // use @Url.Action() to generate your url's
    data: formdata,
    processData: false,
    contentType: false,
    success: function () {
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!