首页 > 解决方案 > 表单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" />

这种方式也不要调用 creat post 方法: 在此处输入图像描述

标签: c#asp.net-corerazorrazor-pages

解决方案


来自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" />

推荐阅读