2017-03-20 45 views
1

我在.net core mvc項目中綁定嵌套的IFormFile時遇到問題。 如果我把IFormFile放在嵌套視圖模型中,它不會在後期綁定它。 例如這不起作用:如果嵌套在視圖模型中,IFormFile未綁定

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> 

一些代碼是爲了簡明扼要省略。

回答

1

我找到了解決方案,所以我想分享給你。我發現它是已知的問題,它應該在.net核心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" />