在asp.net中从视图发送大量数据到控制器

Abd*_*inu 1 c# forms asp.net asp.net-mvc asp.net-core

更具体地说,让我解释一下我遇到了什么。我试图将数据列表从视图提交到控制器。我能够成功提交一些数据,没有任何问题。但当数据超过 250 个项目的列表时,问题就会出现。当我单击提交按钮时,它会在调试时传递 NULL 值。我的代码没有错误,因为我已经向控制器提交了 100 个项目的列表,没有任何问题。我想我必须指定一些东西,以便它也会发送大量列表。在这里,我没有使用 ajax 或任何 javascript 代码来提交表单。我使用发布请求将其直接提交给控制器。

我在下面发布了一些代码片段以更准确地描述它。

看法

    <form method="post" action="SubmitList">
                <div class="row"> 
                    <div class="col-md-12" style="padding-top:1%">
<input type="submit" value="PASS" class="btn btn-primary" style="float:right;" />
                        <div class="box-body">
                            <table id="#example1" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Name</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @{
                                        int i = 1;
                                    }
                                    @for (int j = 0; j < Model.Count(); j++)
                                    {
                                        <tr>
                                            <td>@Html.Raw(i++)</td>
                                            @Html.HiddenFor(item => item[j].Id, new { htmlAttributes = new { @class = "form-control" } })
                                            <td>
                                                @Html.DisplayFor(item => item[j].FullName) 
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                                <tfoot>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </form>
Run Code Online (Sandbox Code Playgroud)

控制器

[AuthorizedAction]
[HttpPost] 
public async Task<IActionResult> SubmitList(List<Student> students)
    {
////
    }
Run Code Online (Sandbox Code Playgroud)

你能告诉我我的代码有什么问题吗?

Roa*_* S. 7

请在 Startup#ConfigureServices 中尝试此操作

services.Configure<FormOptions>(options => options.ValueCountLimit = 1000); // you may want to adjust this limit
Run Code Online (Sandbox Code Playgroud)

参考:https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.features.formoptions

  • 没问题。我刚刚更新了它 - 可能更好地指出官方 MS 文档。 (2认同)