jaz*_*000 3 c# asp.net-mvc model-binding .net-core
此问题出现在.net core 3.1 MVC网站中。
我无法将 POST 绑定到控制器操作(参数始终为空)。数据是从数据库加载的,并且是一个大型的递归结构。如果我删除数据库中的几百行 JSON(大约 2500 行),它将绑定正常。
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>
Run Code Online (Sandbox Code Playgroud)
子视图
<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>
Run Code Online (Sandbox Code Playgroud)
视图模型
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; }
}
Run Code Online (Sandbox Code Playgroud)
控制器 这里 vm 为空,除非我删除一些数据
[HttpPost]
public async Task<IActionResult> CdsDeclarationSectionAndTag(CdsDeclarationSectionAndTagViewModel vm)
{
}
Run Code Online (Sandbox Code Playgroud)
如果我像这样改变它, fc 也为空
[HttpPost]
public async Task<IActionResult> CdsDeclarationSectionAndTag(IFormCollection fc)
{
}
Run Code Online (Sandbox Code Playgroud)
发布的表单数据如下所示(最终可能有 4 或 5 个递归级别)
代码:42A 全名:描述:声明实体[0]。代码:023 实体[0]。全名:接受(税点)日期时间实体[0]。描述:接受(税点)日期时间实体[0]。IsDeleted:假实体[ 1].代码:D026 实体[1].FullName:LRN 实体[1].描述:LRN 实体[1].IsDeleted:假实体[2].代码:D013 实体[2].FullName:声明类型实体[2] ].Description: 声明类型 Entities[2].IsDeleted: false Entities[3].Code: 109 Entities[3].FullName: 发票总实体[3].Description: 发票总实体[3].IsDeleted: false Entities[ 4].Code: 504 Entities[4].FullName: 特定情况指标 Entities[4].Description: 特定情况指标 Entities[4].IsDeleted: false Entities[5].Code: 131 Entities[5].FullName: 总金额质量实体[5].Description:总质量实体[5].IsDeleted:假实体[6].Code:146 个实体[6].FullName:总包实体[6].Description:总包实体[6].IsDeleted : false 实体[7].代码:61B 实体[7].FullName:身份验证实体[7].描述:身份验证实体[7].实体[0].代码:104 实体[7].实体[0].FullName :签名/身份验证实体[7].Entities[0].描述:签名/身份验证实体[7].Entities[0].IsDeleted:假实体[7].IsDeleted:假实体[8].代码:02A实体[ 8].FullName:延期付款实体[8].Description:延期付款实体[8].Entities[0].Code:D031 Entities[8].Entities[0].FullName:延期付款类别代码Entities[8].Entities [0].Description:类别代码 Entities[8].Entities[0].IsDeleted: false Entities[8].Entities[1].Code:D005 Entities[8].Entities[1].FullName:延期 ID 实体[ 8].Entities[1].Description: ID Entities[8].Entities[1].IsDeleted: false Entities[8].Entities[2].Code: D006 Entities[8].Entities[2].FullName: 延期类型实体[8].Entities[2].描述:类型实体[8].Entities[2].IsDeleted:假实体[8].IsDeleted:假实体[9].代码:03A 实体[9].FullName:附加信息实体[9].描述:附加信息实体[9].实体[0].代码:226 实体[9].实体[0].FullName:附加信息声明代码实体[9].实体[0]。说明:语句代码Entities[9].Entities[0].IsDeleted: false ....
jaz*_*000 10
我在这个线程上找到了答案。事实证明,默认情况下,您可以提交的表单值的限制为 1024。
我在 Startup.cs 中使用以下代码更改限制,问题已经消失,现在可以成功绑定
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1403 次 |
| 最近记录: |