亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AJAX POST 數據到 ASP.NET Core 中的 MVC 控制器在傳遞超過一定長度的對

AJAX POST 數據到 ASP.NET Core 中的 MVC 控制器在傳遞超過一定長度的對

牧羊人nacy 2023-04-27 16:46:08
我有一個 javascript 函數,它正在創建一個包含對象數組的模型,并使用 AJAX POST 請求將該數據發送到我的 MVC 控制器。出于某種原因,似乎可以傳遞給控制器的數組的大小有限制,盡管經過實驗,在我的例子中似乎在 450 到 500 個對象之間。一旦它大于該閾值,控制器將只接收一個空值。是否可以通過一些配置或解決方法來消除或解決此限制?我已經嘗試了很多基于 web.config 的“解決方案”,很多人就類似的 SO 問題提出這些解決方案都無濟于事(我認為這些可能僅適用于 .Net Framework 的 ASP.NET 版本,不適用于 .NET Core) .Java腳本:var i = 0;$("#SupplierNumber option").each(function () {    var supplierNumber = {};    supplierNumber.Value = $(this).val();    supplierNumber.text = $(this).val();    supplierNumbers.push(supplierNumber);    //Below limits max size for testing    i = i + 1;    if (i > 450) {        return false;    }});//Other non-array values gathered herevar model = {    "SupplierNumbers": supplierNumbers,    //... other non-array values added to model}$.ajax({    type: "POST",    url: "/Administration/TestPost",    data: model,    dataType: "json",    success: function (data) {        alert("Finished");    }});控制器:[HttpPost]public async Task<IActionResult> TestPost(TestViewModel model){    //model is null here when array is > 500 in size     return Json(new { success = true });}
查看完整描述

5 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

在使用 JSON.stringify 之前試試這個!

我有完全相同的問題。我需要做的就是以下幾點:

在我的 Startup.cs 中,我在ConfigureServices中添加了以下幾行:

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


查看完整回答
反對 回復 2023-04-27
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

我不知道確切的問題是什么,但您可以嘗試此解決方案。在將字符串反序列化為對象之后,嘗試將數據作為刺痛傳遞到操作中。


var model = {

"SupplierNumbers": supplierNumbers,

//... other non-array values added to model

}


$.ajax({

type: "POST",

url: "/Administration/TestPost",

data: {"data":JSON.stringify(model)},

dataType: "json",

success: function (data) {

    alert("Finished");

}

});

在控制器端,您可以將此字符串反序列化以進行建模。


[HttpPost]

public async Task<IActionResult> TestPost(string data)

{

 TestViewModel model=DeserializeObject<TestViewModel>(data);

return Json(new { success = true });

}


查看完整回答
反對 回復 2023-04-27
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

我們需要設置值計數限制和長度。


請在 startup.cs 中添加以下代碼 --- 它對我有用


        services.AddMvc(options =>

        {

            options.MaxModelBindingCollectionSize = 100000;

        });


        services.Configure<FormOptions>(options =>

        {

            options.ValueCountLimit = int.MaxValue;

            options.ValueLengthLimit = int.MaxValue;

            options.MultipartHeadersLengthLimit = int.MaxValue;

        });


查看完整回答
反對 回復 2023-04-27
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

這里有兩個解決方案:


首先demo,使用json類型的數據,ajax中我們通常使用這種方式傳遞array(傳遞json類型的數據,需要contentType: 'application/json'在ajax中使用,在action中使用[FromBody]):


索引視圖模型:


public class IndexViewModel

? ? {

? ? ? ? public int Id { get; set; }

? ? ? ? public string Name { get; set; }

? ? }

控制器:


? ? ? ? [HttpGet]

? ? ? ? public IActionResult TestAjax() {

? ? ? ? ? ? return View();

? ? ? ? }

? ? ? ? [HttpPost]

? ? ? ? public IActionResult TestAjaxWithJsonData([FromBody]List<IndexViewModel> l) {

? ? ? ? ? ? return Ok();

? ? ? ? }

看法:


?<button onclick="postdata1()">submit(jsondata)</button>

? ??

? ??

@section scripts{?

? ? <script type="text/javascript">

? ? ? ? function postdata1() {

? ? ? ? ? ? var a = [];

? ? ? ? ? ? for (var i = 0; i < 500; i++) {

? ? ? ? ? ? ? ? var indexViewModel = {};

? ? ? ? ? ? ? ? indexViewModel.Id = i;

? ? ? ? ? ? ? ? indexViewModel.Name = "name" + i;

? ? ? ? ? ? ? ? a.push(indexViewModel);

? ? ? ? ? ? }

? ? ? ? ? ? var data = JSON.stringify(a);

? ? ? ? ? ? $.ajax({

? ? ? ? ? ? ? ? type: "POST",

? ? ? ? ? ? ? ? url: 'TestAjaxWithJsonData',

? ? ? ? ? ? ? ? data: data,

? ? ? ? ? ? ? ? contentType: 'application/json'

? ? ? ? ? ? }).done(function (data) {

? ? ? ? ? ? });

? ? ? ? }

? ? </script>

}

第二個演示,使用 FormData:


控制器:


[HttpPost]

? ? public IActionResult TestAjaxWithFormdata(List<IndexViewModel> l)

? ? {

? ? ? ? return Ok();

? ? }

看法:


<button onclick="postdata2()">submit(formdata)</button>

? ??

? ??

@section scripts{?

? ? <script type="text/javascript">

? ? ? ??

? ? ? ? function postdata2() {

? ? ? ? ? ? var formdata = new FormData();

? ? ? ? ? ? for (var i = 0; i < 500; i++) {

? ? ? ? ? ? ? ? formdata.append("l[" + i + "].Id", i);

? ? ? ? ? ? ? ? formdata.append("l[" + i + "].Name", "name" + i);

? ? ? ? ? ? }

? ? ? ? $.ajax({

? ? ? ? ? ? ? ? type: "POST",

? ? ? ? ? ? ? ? url: 'TestAjaxWithFormdata',

? ? ? ? ? ? ? ? cache: false,

? ? ? ? ? ? ? ? contentType: false,

? ? ? ? ? ? ? ? processData: false,

? ? ? ? ? ? ? ? data: formdata,

? ? ? ? ? ? ?}).done(function (data) {

? ? ? ? });

? ? ? ? }

? ? ? ??

? ? </script>

}


查看完整回答
反對 回復 2023-04-27
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

經過更多搜索后,我發現這是 ASP.NET Core 中的配置問題。默認情況下,復雜模型中可以綁定的最大值數為 1024。在 Startup 類的 ConfigureServices 方法中添加以下選項將允許您將此限制增加到您需要的任何值 。

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

另外,我發現人們在面對這個問題時可能會發現有用的其他東西,但是使用表單提交操作,是下面的選項,它采用與上面提到的相同的方法(source)。

services.Configure<FormOptions>(options?=>?options.ValueCountLimit?=?100000);


查看完整回答
反對 回復 2023-04-27
  • 5 回答
  • 0 關注
  • 341 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號