Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IFormFile is not bound if nested in view model

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.

like image 542
Mariusz Avatar asked Sep 10 '25 18:09

Mariusz


1 Answers

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" />
like image 131
Mariusz Avatar answered Sep 13 '25 13:09

Mariusz