ASP.NET 核心 - 控制器未从 Ajax 接收完整的 JavaScript 数组对象

ASP.NET Core - Controller Not Receiving Full JavaScript Array Object from Ajax

我有一个 JS 对象数组,我将 ajax 传递给控制器​​。如果我在数组中传递最多 11 个对象,控制器会接收到该数组,但如果对象超过 11 个,它会将数组接收为 null。就这个。

在controller中,我会收到传过来的employee数组,如下图。

但是如果我传递一个大 JavaScript 数组对象,它的值为 null。

我有一个包含 50 个对象的数组。我已经测试了所有这些。该对象与模型属性匹配。模态没有问题。

请告诉我我做错了什么。

这是我的 JS 代码。 基本上,我正在尝试读取一个 csv 文件并将所有数据推送到 javascript 数组。 javascript 数组具有与模型字段名称相同的属性。

let employees = [];
let reader = new FileReader();

$("#btnSave").click(function () {
    Save();
})

$("#file").change(function (e) {
    //getting the uploaded file
    let fileUpload = $("#file").get(0)
    let file = fileUpload.files[0];
    //load the reader
    reader.onloadend = function (evt) {
        let lines = evt.target.result;
        if (lines && lines.length > 0) {
            let allRows = CSVToArray(lines);
            let header = allRows[0];
            let thead = $('thead tr')
            thead.html("")
            let tbody = $('tbody')
            header.forEach(h => thead.append(`<th>${h}</th>`))
            for (let i = 1; i < allRows.length; i++) {
                tbody.append('<tr>')
                let cellArr = allRows[i];
                let emp = {}
                for (let k = 0; k < cellArr.length; k++) {
                    emp[allRows[0][k]] = cellArr[k] + "";
                    tbody.append(`<td>${cellArr[k]}</td>`)
                }
                tbody.append('</tr>')
                employees.push(emp)
            }
        }
    }
    reader.readAsBinaryString(file)
})

function CSVToArray(strData, strDelimiter) {
    strDelimiter = (strDelimiter || ",");
    let objPattern = new RegExp(
        (
            "(\" + strDelimiter + "|\r?\n|\r|^)" +
            "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
            "([^\"\" + strDelimiter + "\r\n]*))"
        ),
        "gi"
    );
    let arrData = [[]];
    let arrMatches = null;

    while (arrMatches = objPattern.exec(strData)) {
        let strMatchedDelimiter = arrMatches[1];
        let strMatchedValue = [];
        if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) {
            arrData.push([]);
        }
        if (arrMatches[2]) {
            strMatchedValue = arrMatches[2].replace(new RegExp("\"\"", "g"), "\"");
        } else {
            strMatchedValue = arrMatches[3];
        }
        arrData[arrData.length - 1].push(strMatchedValue);
    }
    return (arrData);
}

function Save() {
    console.log( employees)
    if (employees.length > 0) {
        $.ajax({
            url: '/Master/SaveMaster',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(employees),
            success: function (result) {
                /*for (let k = 0; k < result.length; k++) {
                    $('tbody tr:eq(' + (result[k].employeeId - 1) + ')').css('border', '3px solid #dc3545');
                    let props = Object.entries(employees[result[k].employeeId])
                    console.log(result[k])
                    let cellIndex = props.findIndex(p => p[0] === result[k].field)
                    $('tbody tr:eq(' + (result[k].employeeId - 1) + ') td:eq(' + cellIndex + ')').css({ 'background-color': '#dc3545', 'color': '#ffffff' });
                    $('tbody tr:eq(' + (result[k].employeeId - 1) + ') td:eq(' + cellIndex + ')').attr("title", result[k].error)
                }
                $(".main-container").append(`
                       <div class="alert alert-success alert-dismissible fade show" role="alert">
                           Data Successfully imported into the database
                       </div>
                    `)*/
            },
            error: function (result) {
                console.log(result)
            }
        })
    }
    else {
        alert("No Data Found.");
    }
}

这是我的控制器

        [Route("/Master/SaveMaster")]
        [HttpPost]
        public List<MasterError> SaveMaster(List<MasterData> masterData)
        {
            List<MasterError> masterErrorsList = new List<MasterError>();

            //for(int i = 0; i < masterData.Count(); i++)
            //{
                //Employee employee = new Employee();
                //employee.ArabicName = masterData[i].ArabicName;
                //employee.Code = masterData[i].Code;
                //employee.Email = masterData[i].Email;
                //employee.FirstName = masterData[i].FirstName;
                //employee.LastName = masterData[i].LastName;
                //employee.MiddleName = masterData[i].MiddleName;
                //employee.Mobile = masterData[i].Mobile;
                //employee.PassportName = masterData[i].PassportName;
                //employee.PassportNo = masterData[i].PassportNo;
                //employee.ResidentId = masterData[i].QatarID;
                //checkModel(employee);
            //}
            return masterErrorsList;
        }

上面的答案对我不起作用(可能是我的数组的大小更大)但下面的答案起作用了。

[Route("/Master/SaveMaster")]
[HttpPost]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
public List<MasterError> SaveMaster([FromForm] List<MasterData> masterData)

这就是我为使其完美运行所必须做的一切!