0

I am using formData.append("PermissionList", ["Owner", "Resident"]); to pass an array of strings to the controller. I am receiving the value as a string[] with only 1 value and it looks like this: Owner,Resident. How can I pass an array as a FormData?

This is the JavaScript code:

var formData = new FormData();
var files = $("#file").get(0).files;
if (files.length > 0) { 
        formData.append("ReportFile", files[0]);
    }
    else {
        alert("No file is chosen");
        return;
    }
    formData.append("PermissionList", ["Owner","Resident"]);
    formData.append("ReportName", $('#report-name-input').val());

    $.ajax({
        cache: false,
        url: 'Upload',
        type: 'POST',
        async: true,
        traditional: true,
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
            window.location = data;
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(thrownError);
        }
    });

This is how i receive it in the controller:

    public ActionResult Upload(HttpPostedFileBase ReportFile, string ReportName, string[] PermissionList)
    {
        Debug.WriteLine("hey " + ReportFile.FileName + " " + ReportName + " " + PermissionList.Length + " " + PermissionList[0]);

        return Content(Url.Action("Index", "Reporting"));
    }

The output is:

hey MonthlyReport.rdl OtherReport 1 Owner,Resident
A. Savva
  • 632
  • 10
  • 30

0 Answers0