【问题标题】:Parameter sent to ASP NET Core Controller from AJAX call is null if it's too large?从 AJAX 调用发送到 ASP NET Core 控制器的参数是否太大?
【发布时间】:2022-08-17 01:49:13
【问题描述】:

我的页面中有调用 ASP.NET Core 控制器的 AJAX 代码。该代码将对象列表发送到控制器。当列表足够短时,比如 8 个对象,fundFindingsGridRows 参数被正确设置为数据,但是,当列表更长时,此参数为空。

我尝试在我的 Startup.cs 中设置一些东西,但没有任何效果。我可以配置其他一些设置来让它接受更多的数据吗?除了尺寸之外还有其他问题吗?

Startup.cs(相关代码):

        services.AddMvc(options =>
        {
            options.MaxModelBindingCollectionSize = 100000;
        });

        services.Configure<FormOptions>(options =>
        {
            options.ValueCountLimit = int.MaxValue;
            options.ValueLengthLimit = int.MaxValue;
            options.MultipartHeadersLengthLimit = int.MaxValue;
        });

        services.Configure<IISServerOptions>(options =>
        {
            options.MaxRequestBodySize = int.MaxValue;
        });

Javascript AJAX 代码:

            var DATA = new Array();
            var grid = $(\"#V3FundFindingsByBuildingGrid\").data(\"kendoGrid\");
            var dataTable = grid.dataSource;

            $.each(grid.items(), function (index, item) {
                var id = $(item).data(\'uid\');
                var dataItem = dataTable.getByUid(id);
                var building = {};

                building.PANumber = dataItem.PANumber,
                building.employerNo = dataItem.employerNo,
                building.billToEntityNo = dataItem.billToEntityNo,
                building.accountNo = dataItem.AccountNo,
                building.revisionDateExists = @Model.revisionDateExists.ToString().ToLower(),
                building.settlement = false,
                building.health  = dataItem.Health,
                building.pension = dataItem.Pension,
                building.annuity = dataItem.Annuity,
                building.legal = dataItem.Legal,
                building.training = dataItem.Training,
                building.joint = dataItem.Joint,
                building.four01k  = dataItem.Four01k,
                building.healthInterest = dataItem.HealthInterest,
                building.pensionInterest = dataItem.PensionInterest,
                building.annuityInterest = dataItem.AnnuityInterest,
                building.legalInterest = dataItem.LegalInterest,
                building.trainingInterest = dataItem.TrainingInterest,
                building.jointInterest = dataItem.JointInterest,
                building.four01kInterest  = dataItem.Four01kInterest

                DATA.push(building);
            });

            var fundFindingsGridRows = JSON.stringify(DATA);

            $.ajax({
                type: \"POST\",
                url: \"/PayrollAudit/SaveFundFindings\",
                data: fundFindingsGridRows,
                contentType: \"application/json; charset=utf-8\",
                dataType: \"json\",
                success: function (response) {
                    $(\'#FindingsByBuildingDiv\').html(response);
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });

控制器动作:

[RequestSizeLimit(100_000_000)]    
public IActionResult SaveFundFindings([FromBody]List<FundFindingsGridRow> fundFindingsGridRows)
        {...}

来自标题的数据:

解析的有效载荷 sn-p:

    标签: javascript asp.net-core asp.net-ajax


    【解决方案1】:

    所以我想办法解决这个问题。我所做的是不将数据作为 JSON 发送到控制器,而不是作为对象数组。我还从 AJAX 调用中删除了 contentTypedataType 设置,并更改了 data 设置:

                var DATA = new Array();
                var grid = $("#V3FundFindingsByBuildingGrid").data("kendoGrid");
                var dataTable = grid.dataSource;
    
                $.each(grid.items(), function (index, item) {
                    var id = $(item).data('uid');
                    var dataItem = dataTable.getByUid(id);
                    var FundFindingsGridRow = {};
    
                    FundFindingsGridRow.PANumber = dataItem.PANumber,
                    FundFindingsGridRow.employerNo = dataItem.employerNo,
                    FundFindingsGridRow.billToEntityNo = dataItem.billToEntityNo,
                    FundFindingsGridRow.accountNo = dataItem.AccountNo,
                    FundFindingsGridRow.revisionDateExists =        @Model.revisionDateExists.ToString().ToLower(),
                    FundFindingsGridRow.settlement = false,
                    FundFindingsGridRow.health  = dataItem.Health,
                    FundFindingsGridRow.pension = dataItem.Pension,
                    FundFindingsGridRow.annuity = dataItem.Annuity,
                    FundFindingsGridRow.legal = dataItem.Legal,
                    FundFindingsGridRow.training = dataItem.Training,
                    FundFindingsGridRow.joint = dataItem.Joint,
                    FundFindingsGridRow.four01k  = dataItem.Four01k,
                    FundFindingsGridRow.healthInterest = dataItem.HealthInterest,
                    FundFindingsGridRow.pensionInterest = dataItem.PensionInterest,
                    FundFindingsGridRow.annuityInterest = dataItem.AnnuityInterest,
                    FundFindingsGridRow.legalInterest = dataItem.LegalInterest,
                    FundFindingsGridRow.trainingInterest = dataItem.TrainingInterest,
                    FundFindingsGridRow.jointInterest = dataItem.JointInterest,
                    FundFindingsGridRow.four01kInterest  = dataItem.Four01kInterest
    
                    DATA.push(FundFindingsGridRow);
                });
    
                $.ajax({
                    type: "POST",
                    url: "/PayrollAudit/SaveFundFindings",
                    data: { 'fundFindingsGridRows': DATA },
                    success: function (response) {
                        $('#FindingsByBuildingDiv').html(response);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });

    不确定 JSON 的问题是什么。如果有人可以让我知道,我将不胜感激,以防我将来遇到需要 JSON 的实例!

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 2020-11-19
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多