I am having problem with binding nested IFormFile in .net core mvc project.
If I put my IFormFile
in nested view model it will not be bound to it on post.
For example this does not work:
public class SomeVM
{
public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
public IFormFile UploadingImage { get; set; }
//gallery properties...
}
View:
@model SomeVM
<form method="post" enctype="multipart/form-data">
<input type="file" name="Gallery.UploadingImage" />
<input type="submit" value="save" />
</form>
Some code was omitted for brevity.
I found the solution to that so I want to share it with you. I found that it is known issue and it should be solved in .net core 2.0 issue on github
Current hack is to send some extra data when uploading file.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With