如果嵌套在视图模型中,则 IFormFile 未绑定
IFormFile is not bound if nested in view model
我在 .net core mvc 项目中绑定嵌套的 IFormFile 时遇到问题。
如果我将 IFormFile
放在嵌套视图模型中,它将不会绑定到 post。
例如,这不起作用:
public class SomeVM
{
public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
public IFormFile UploadingImage { get; set; }
//gallery properties...
}
查看:
@model SomeVM
<form method="post" enctype="multipart/form-data">
<input type="file" name="Gallery.UploadingImage" />
<input type="submit" value="save" />
</form>
为简洁起见省略了一些代码。
我找到了解决方法,所以我想与您分享。我发现这是一个已知问题,应该在 .net core 2.0 issue on github
中解决
当前的破解方法是在上传文件时发送一些额外的数据。
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" />
我在 .net core mvc 项目中绑定嵌套的 IFormFile 时遇到问题。
如果我将 IFormFile
放在嵌套视图模型中,它将不会绑定到 post。
例如,这不起作用:
public class SomeVM
{
public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
public IFormFile UploadingImage { get; set; }
//gallery properties...
}
查看:
@model SomeVM
<form method="post" enctype="multipart/form-data">
<input type="file" name="Gallery.UploadingImage" />
<input type="submit" value="save" />
</form>
为简洁起见省略了一些代码。
我找到了解决方法,所以我想与您分享。我发现这是一个已知问题,应该在 .net core 2.0 issue on github
中解决当前的破解方法是在上传文件时发送一些额外的数据。
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" />