c# - 表单asp net core上的动态字段
问题描述
我需要一些帮助来解决这个问题,我不知道如何弄清楚。基本上我想在表单上添加动态字段,但我无法绑定 Submit Post 上的值。该列表始终为空,没有值。
基本上我有一个包含一些字段和一个列表的视图模型。
我的 PlanoAcaoViewModel:
public class PlanoAcaoViewModel
{
public int IdPlanoAcao { get; set; }
public int Numero { get; set; }
public DateTime DataEncerramento { get; set; }
public List<PlanoEvidenciaIForm> ListaPlanoEvidenciaIForm { get; set; }
public class PlanoEvidenciaIForm
{
public int IdPlanoAcaoEvidencia { get; set; }
public IFormFile Arquivo { get; set; }
public string Nota { get; set; }
public DateTime Data { get; set; }
}
}
我的控制器:
[HttpPost]
public async Task<IActionResult> Create(PlanoAcaoViewModel model)
{
var num = model.Numero; // It's ok, return the right number
var data = model.DataEncerramento ; // It's ok, return the right date
foreach (var it in model.ListaPlanoEvidenciaIForm)
{
// model.ListaPlanoEvidenciaIForm is NULL
}
}
我的cshtml:
@model ProjetoGestor.ViewModel.PlanoAcaoViewModel
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row">
<div class="col-auto">
<label asp-for="Numero" class="control-label"></label>
<input asp-for="Numero" class="form-control" disabled type="text" />
</div>
<div class="col-auto">
<label asp-for="DataEncerramento" class="control-label"></label>
<input asp-for="DataEncerramento" class="form-control" type="date" placeholder="dd/mm/yyyy" />
<span asp-validation-for="DataEncerramento" class="text-danger"></span>
</div>
</div>
<div class="row">
@* Dynamic Fields *@
<input asp-for="ListaPlanoEvidenciaIForm[0].Arquivo" class="form-control" type="file" />
<input asp-for="ListaPlanoEvidenciaIForm[0].Nota" class="form-control" />
</div>
<div class="row col-auto">
<div class="align-center">
<a class="btn btn-primary btn-xl" asp-action="Index">Voltar</a> |
<input type="submit" value="Criar" class="btn btn-primary btn-success btn-xl" />
</div>
</div>
</form>
而动态字段我已经尝试了很多方法但没有成功:
// In this way the model.ListaPlanoEvidenciaIForm is NULL
<input asp-for="ListaPlanoEvidenciaIForm[0].Arquivo" class="form-control" type="file" />
<input asp-for="ListaPlanoEvidenciaIForm[0].Nota" class="form-control" />
// In this way don't do it the POST (don't call the method create post)
<input name="ListaPlanoEvidenciaIForm[0].Arquivo" class="form-control" type="file" />
<input name="ListaPlanoEvidenciaIForm[0].Nota" class="form-control" />
// In this way the model.ListaPlanoEvidenciaIForm have length > 0 but all values inside list are null
<input name="ListaPlanoEvidenciaIForm" class="form-control" type="file" />
<input name="ListaPlanoEvidenciaIForm" class="form-control" />
解决方案
来自https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1;
当使用模型绑定和上传文件时
IFormFile
,action 方法可以接受:
- 一个
IFormFile
.- 以下任何代表多个文件的集合:
IFormFileCollection
IEnumerable<IFormFile>
List<IFormFile>
没有提到对象列表,每个对象都包含一个IFormFile
. 因此,您可能遇到了 MVC 绑定的限制。我建议您尝试将文件列表与其他值分开;
public List<PlanoEvidenciaIForm> ListaPlanoEvidenciaIForm { get; set; }
public List<IFormFile> Arquivos { get; set; }
public class PlanoEvidenciaIForm
{
public int IdPlanoAcaoEvidencia { get; set; }
public string Nota { get; set; }
public DateTime Data { get; set; }
}
<!-- The IFormFile doesn't use [ ] -->
<input asp-for="Arquivos" class="form-control" type="file" />
<input asp-for="ListaPlanoEvidenciaIForm[0].Nota" class="form-control" />
推荐阅读
- java - 使用 Spring Boot 2.0.2 的 OpenTracing 在 Jaeger 中不会产生任何跟踪
- selenium - 机器人框架 - 清除元素文本关键字不起作用
- javascript - 未调用超测异步回调
- azure - 从自定义活动 Powershell 脚本复制输出数据
- ruby - 动态扩展 ruby 编译器
- typo3 - TYPO3 tx_news 问题通过添加自定义字段
- java - groovy - javafx - 使用 WebLogic InitialContext 后无法从 FXML 打开新窗口
- android - 如何解决 BLE 设备在特定时间后断开连接?
- rpg - IBM RPG 文件到 C# 的转换
- django - 从 Django 管理面板点击 API