表单 asp net core 上的动态字段

Dynamic fields on form asp net core

我需要一些帮助来解决这个问题,我不知道如何解决。 基本上我想在表单上添加动态字段,但我无法绑定提交 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" />

这样也不要调用creat方法post:

来自 https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1;

When uploading files using model binding and IFormFile, the action method can accept:

  • A single IFormFile.
  • Any of the following collections that represent several files:
    • 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" />