如何将所选值与上载的IFormFile相关联

在ASP.Net Core和JQuery中,我试图将“选择列表”中的选定值与要上传的IFormFile相关联。选择列表选项是类别列表,上载的文件需要属于其中一个。可能有多个IFormFiles与表单提交一起提交,但是每个文件都必须使用所选类别的ID进行分类(不是多选,而是几个单个文件)。

我正在使用JQuery .each()来获取IFormFile值和categoryId,但是当击中C#控制器方法时categoryId的值为null。

--JQuery--
$(':file').each(function (index,field) {
   var file = field.files[0];
   formData.append('files',file);
   var attachmentTypeId = $(this).parent().parent().find('option:selected').val();
   formData.append('attachments.Attachment.AttachmentTypeId',attachmentTypeId);
});
--C# ViewModel--
public Attachment Attachment { get; set; }
public List<Attachment> Attachments = new List<Attachment>();
public IFormFile File { get; set; }
public List<IFormFile> Files { get; set; }
--C# Attachment Class--
public int AttachmentTypeId { get; set; }
public string FileName { get; set; }
public byte[] FileContent { get; set; }
public IFormFile FormFile { get; set; }
--C# Controller Method--
public async Task<IactionResult> Create(ViewModel viewModel)
{
   if (viewModel.Files.Count > 0 || viewModel.File != null)
   {
      foreach (var file in viewModel.Files)
      {
         Attachment attachment = new Attachment()
         {
            FileName = viewModel.File.FileName,AttachmentTypeId = viewModel.Attachment.AttachmentTypeId
         };

         using (var ms = new MemoryStream())
         {
            viewModel.File.CopyTo(ms);
            var fileBytes = ms.ToArray();
            string s = Convert.ToBase64String(fileBytes);
            attachment.FileContent = fileBytes;
         }

         path = $"Attachments/Create";
         content = new JsonContent(attachment);
         await _client.PostData(path,content);
      }
   }

   return Ok();
}

我正在尝试从“选择列表”中的“选择的列表”中获取所选的值到JQuery FormData对象中(此方法有效),并将其通过AJAX调用传递给C#控制器方法(除所选ID之外的所有方法)。 >

q276941897 回答:如何将所选值与上载的IFormFile相关联

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3163568.html

大家都在问