5 回答

TA貢獻1835條經驗 獲得超7個贊
在使用 JSON.stringify 之前試試這個!
我有完全相同的問題。我需要做的就是以下幾點:
在我的 Startup.cs 中,我在ConfigureServices中添加了以下幾行:
services.Configure<FormOptions>(options => { options.ValueCountLimit = int.MaxValue; options.ValueLengthLimit = int.MaxValue; });

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 });
}

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;
});

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>
}

TA貢獻1817條經驗 獲得超14個贊
經過更多搜索后,我發現這是 ASP.NET Core 中的配置問題。默認情況下,復雜模型中可以綁定的最大值數為 1024。在 Startup 類的 ConfigureServices 方法中添加以下選項將允許您將此限制增加到您需要的任何值 。
services.AddMvc(options?=>{ ???options.MaxModelBindingCollectionSize?=?100000; })
另外,我發現人們在面對這個問題時可能會發現有用的其他東西,但是使用表單提交操作,是下面的選項,它采用與上面提到的相同的方法(source)。
services.Configure<FormOptions>(options?=>?options.ValueCountLimit?=?100000);
添加回答
舉報