gpt4 book ai didi

c# - 对于 MVC Model Binder,表单 POST 是否太大

转载 作者:行者123 更新时间:2023-12-05 09:37:14 25 4
gpt4 key购买 nike

这个问题出现在.net core 3.1 MVC网站。

我无法让我的 POST 绑定(bind)到我的 Controller 操作(参数始终为空)。数据是从数据库加载的,是一个大的递归结构。如果我在数据库中删除几百行 JSON(大约 2500 行),它会绑定(bind) OK。

GET 显示完美。

即使我将我的 Action 方法参数从我的 ViewModel 更改为 IFormCollection,它仍然以 null 形式出现。这里有一些我不知道的限制吗?如果大小是问题,是否有更好的方法来发布数据?

父 View

<form id="frmAdditionalCodes" name="frmAdditionalCodes" method="post">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Code)
</th>
<th>
@Html.DisplayNameFor(model => model.FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
</thead>
<tbody>

<tr>

<td style="width:50px">
@Html.DisplayFor(modelItem => Model.Code)
<input asp-for="@Model.Code" class="form-control" style="display:none" />
</td>
<td style="width:50px">
@Html.DisplayFor(modelItem => Model.FullName)
<input asp-for="@Model.FullName" class="form-control" style="display:none" />
</td>
<td style="width:50px">
@Html.DisplayFor(modelItem => Model.Description)
<input asp-for="@Model.Description" class="form-control" style="display:none" />
</td>
<td style="width:20px">
<span>
<i id="addTagItem" class="fas fa-folder-plus" title="Add child item"></i>
</span>

</td>
<td>
<partial name="~/Views/PartialViews/_SectionsAndTags.cshtml" model="@Model.Entities" view-data="ViewData" />
</td>
<td>
</td>

</tr>

</tbody>
</table>
</form>

subview

<table class="partial_table">
<thead>
<tr>

<th>
@Html.DisplayNameFor(model => model.Code)
</th>
<th>
@Html.DisplayNameFor(model => model.FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th colspan="2">
@Html.DisplayNameFor(model => model.Entities)
</th>
</tr>
</thead>
<tbody>
@{ List<CdsDeclarationSectionAndTagItemViewModel> list = Model.ToList();
for (int i = 0; i < list.Count(); i++)
{
<tr>
@{
string elementNameCode = $"{list[i].Prefix}Code";
string elementNameFullName = $"{list[i].Prefix}FullName";
string elementNameDescription = $"{list[i].Prefix}Description";
string elementNameIsDeleted = $"{list[i].Prefix}IsDeleted";
}
<td>
<span>@list[i].Code</span>
<input asp-for="@list[i].Code" name="@elementNameCode" class="form-control" style="display: none" />
</td>
<td>
<span>@list[i].FullName</span>
<input asp-for="@list[i].FullName" class="form-control" name="@elementNameFullName" style="display: none" />
</td>
<td>
<span>@list[i].Description</span>
<input asp-for="@list[i].Description" class="form-control" name="@elementNameDescription" style="display: none" />
</td>
<td>
@if (list[i].Entities?.Length > 0)
{<img id="collapseItem" state="expanded" width="20" height="20" src="~/images/minus_PNG24.png" />
}

<span>
<i id="editTagItem" class="fas fa-pencil-alt" title="Edit item"></i>
<i id="deleteTagItem" class="far fa-trash-alt" title="Delete item"></i>
<i id="addTagItem" class="fas fa-folder-plus" title="Add child item"></i>
<i id="updateTagItem" class="far fa-save" title="Save changes" style="display: none"></i>
<i id="cancelTagItem" class="fas fa-undo-alt" title="Undo changes" style="display: none"></i>
</span>

</td>
<td>
@if (list[i].Entities?.Length > 0)
{
<partial name="~/Views/PartialViews/_SectionsAndTags.cshtml" model="@list[i].Entities" />
}
</td>
<td>
<input type="hidden" value="false" name="@elementNameIsDeleted" />

</td>

</tr>
}
}
</tbody>
</table>

View 模型

public class CdsDeclarationSectionAndTagViewModel
{
public string Code { get; set; }

public string FullName { get; set; }

public string Description { get; set; }

public CdsDeclarationSectionAndTagItemViewModel[] Entities { get; set; }
}

public class CdsDeclarationSectionAndTagItemViewModel
{
public string Code { get; set; }
public string FullName { get; set; }
public string Description { get; set; }

public CdsDeclarationSectionAndTagItemViewModel[] Entities { get; set; }

public string Prefix { get; set; }

public bool IsDeleted { get; set; }
}

Controller 这里 vm 是空的,除非我删除一些数据

[HttpPost]
public async Task<IActionResult> CdsDeclarationSectionAndTag(CdsDeclarationSectionAndTagViewModel vm)
{
}

如果我这样改,fc也是null

[HttpPost]
public async Task<IActionResult> CdsDeclarationSectionAndTag(IFormCollection fc)
{
}

发布的表单数据看起来像这样(并且可以以 4 或 5 个递归级别结束)

Code: 42AFullName:Description: DeclarationEntities[0].Code: 023Entities[0].FullName: Acceptance (taxpoint) datetimeEntities[0].Description: Acceptance (taxpoint) datetimeEntities[0].IsDeleted: falseEntities[1].Code: D026Entities[1].FullName: LRNEntities[1].Description: LRNEntities[1].IsDeleted: falseEntities[2].Code: D013Entities[2].FullName: Declaration typeEntities[2].Description: Declaration typeEntities[2].IsDeleted: falseEntities[3].Code: 109Entities[3].FullName: Invoice totalEntities[3].Description: Invoice totalEntities[3].IsDeleted: falseEntities[4].Code: 504Entities[4].FullName: Specific circumstances indicatorEntities[4].Description: Specific circumstances indicatorEntities[4].IsDeleted: falseEntities[5].Code: 131Entities[5].FullName: Gross massEntities[5].Description: Gross massEntities[5].IsDeleted: falseEntities[6].Code: 146Entities[6].FullName: Total packagesEntities[6].Description: Total packagesEntities[6].IsDeleted: falseEntities[7].Code: 61BEntities[7].FullName: AuthenticationEntities[7].Description: AuthenticationEntities[7].Entities[0].Code: 104Entities[7].Entities[0].FullName: Signature/AuthenticationEntities[7].Entities[0].Description: Signature/AuthenticationEntities[7].Entities[0].IsDeleted: falseEntities[7].IsDeleted: falseEntities[8].Code: 02AEntities[8].FullName: Deferred PaymentEntities[8].Description: Deferred PaymentEntities[8].Entities[0].Code: D031Entities[8].Entities[0].FullName: Deferment category codeEntities[8].Entities[0].Description: Category codeEntities[8].Entities[0].IsDeleted: falseEntities[8].Entities[1].Code: D005Entities[8].Entities[1].FullName: Deferment IDEntities[8].Entities[1].Description: IDEntities[8].Entities[1].IsDeleted: falseEntities[8].Entities[2].Code: D006Entities[8].Entities[2].FullName: Deferment TypeEntities[8].Entities[2].Description: TypeEntities[8].Entities[2].IsDeleted: falseEntities[8].IsDeleted: falseEntities[9].Code: 03AEntities[9].FullName: Additional InformationEntities[9].Description: Additional InformationEntities[9].Entities[0].Code: 226Entities[9].Entities[0].FullName: Additional Information Statement codeEntities[9].Entities[0].Description: Statement codeEntities[9].Entities[0].IsDeleted: false....

最佳答案

我在 this 上找到了答案线。事实证明,默认情况下,您可以提交的表单值限制为 1024。

我在 Startup.cs 中使用了以下代码来更改限制,问题消失了,现在可以成功绑定(bind)

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

关于c# - 对于 MVC Model Binder,表单 POST 是否太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64458418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com