Razor 如果在视图模型中嵌套,则不会绑定文件

Razor 如果在视图模型中嵌套,则不会绑定文件,razor,asp.net-core-mvc,.net-core,model-binding,Razor,Asp.net Core Mvc,.net Core,Model Binding,我在.net核心mvc项目中绑定嵌套文件时遇到问题。 如果我把我的ifformfile放在嵌套视图模型中,它将不会在post上绑定到它。 例如,这不起作用: public class SomeVM { public GalleryVM Gallery { get; set; } } public class GalleryVM { public IFormFile UploadingImage { get; set; } //gallery properties... } 视图:

我在.net核心mvc项目中绑定嵌套文件时遇到问题。 如果我把我的
ifformfile
放在嵌套视图模型中,它将不会在post上绑定到它。 例如,这不起作用:

public class SomeVM
{
  public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
  public IFormFile UploadingImage { get; set; }
  //gallery properties...
}
视图:

@modelsomevm

为了简洁起见,省略了一些代码。

我找到了解决方案,因此我想与您分享。我发现这是一个已知的问题,应该在.NETCore2.0中解决

当前的黑客是在上传文件时发送一些额外的数据

public class SomeVM
{
  public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
  public IFormFile UploadingImage { get; set; }
  public bool FormFileHack { get; set; }
  //gallery properties...
}

//the view .cshtml

<input type="file" name="Gallery.UploadingImage" />
<input type="hidden" name="Gallery.FormFileHack" />
公共类SomeVM
{
公共GalleryVM库{get;set;}
}
公共级帆船
{
公共文件上载映像{get;set;}
公共bool FormFileHack{get;set;}
//画廊属性。。。
}
//cshtml

您好,我在.NET Core 3.1中发现了一个小的错误。嵌套文件“单独”会导致整个父对象中出现空值。将其与周围的“fake/hack”属性嵌套会导致文件中出现空值。最好的是,Request.Form.Files[0]中始终存在正确的文件,因此前端和后端之间的映射是正确的。。有人知道如何克服这个问题吗?
public class SomeVM
{
  public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
  public IFormFile UploadingImage { get; set; }
  public bool FormFileHack { get; set; }
  //gallery properties...
}

//the view .cshtml

<input type="file" name="Gallery.UploadingImage" />
<input type="hidden" name="Gallery.FormFileHack" />