如何解决对于MVC Model Binder,表格POST可能太大吗?
.net core 3.1 MVC网站中会出现此问题。
我无法让POST绑定到我的控制器动作(参数始终以null表示)。数据是从数据库加载的,并且是大型的递归结构。 如果我在数据库中删除几百行JSON(2500条左右),它将绑定OK。
GET完美显示。
即使将Action方法参数从viewmodel更改为IFormCollection,它也仍然为null。这里有我不知道的限制吗? 如果大小是问题,是否有更好的方法来发布数据?
父视图
<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>
子视图
<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>
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; }
}
控制器 除非我删除一些数据,否则vm为null
[HttpPost]
public async Task<IActionResult> CdsDeclarationSectionAndTag(CdsDeclarationSectionAndTagviewmodel vm)
{
}
如果我这样更改它,则fc也为空
[HttpPost]
public async Task<IActionResult> CdsDeclarationSectionAndTag(IFormCollection fc)
{
}
发布的表单数据看起来像这样(最终可以达到4或5个递归级别)
代码:42A 全名: 说明:声明 实体[0]。代码:023 实体[0]。全名:接受(税点)日期时间 实体[0]。描述:接受(税点)日期时间 实体[0]。已删除:false 实体[1]。代码:D026 实体[1]。全名:LRN 实体[1]。描述:LRN 实体[1]。已删除:false 实体[2]。代码:D013 实体[2]。全名:声明类型 实体[2]。描述:声明类型 实体[2]。已删除:false 实体[3]。代码:109 实体[3]。全名:发票总额 实体[3]。描述:发票总额 实体[3]。已删除:false 实体[4]。代码:504 实体[4]。全名:具体情况指标 实体[4]。描述:特定情况指示器 实体[4]。已删除:false 实体[5]。代码:131 实体[5]。全名:毛重 实体[5]。描述:毛重 实体[5]。已删除:false 实体[6]。代码:146 实体[6]。全名:软件包总数 实体[6]。描述:总包装数 实体[6]。已删除:false 实体[7]。代码:61B 实体[7]。全名:身份验证 实体[7]。描述:身份验证 实体[7]。实体[0]。代码:104 实体[7]。实体[0]。全名:签名/身份验证 实体[7]。实体[0]。描述:签名/认证 实体[7]。实体[0]。已删除:false 实体[7]。已删除:false 实体[8]。代码:02A 实体[8]。全名:延期付款 实体[8]。描述:延期付款 实体[8]。实体[0]。代码:D031 实体[8]。实体[0]。全名:延期类别代码 实体[8]。实体[0]。描述:类别代码 实体[8]。实体[0]。已删除:false 实体[8]。实体[1]。代码:D005 实体[8]。实体[1]。全名:延期ID 实体[8]。实体[1]。描述:ID 实体[8]。实体[1]。已删除:false 实体[8]。实体[2]。代码:D006 实体[8]。实体[2]。全名:延期类型 实体[8]。实体[2]。描述:类型 实体[8]。实体[2]。已删除:false 实体[8]。已删除:false 实体[9]。代码:03A 实体[9]。全名:附加信息 实体[9]。描述:附加信息 实体[9]。实体[0]。代码:226 实体[9]。实体[0]。全名:附加信息语句代码 实体[9]。实体[0]。描述:语句代码 实体[9]。实体[0]。已删除:false ....
解决方法
我在this线程中找到了答案。 事实证明,默认情况下,您可以提交的表单值限制为1024。
我在Startup.cs中使用以下代码更改了限制,问题已经消失,现在可以成功绑定
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。