Asp.Net core 3.1 not binding file to IFormFile property inside model

Viewed 1058

I have an http post request in my controller that takes a model with an IFormFile property:

 public ActionResult Post([FromForm] PageModel pageModel)
 {
           // process data
 }

The pageModel class:

    public class PageModel 
    {
        public Guid Id { get; set; }  
        public string Title { get; set; }  
        public Image ImageModel { get; set; }
    }

    public class ImageModel
    {
        public Guid Id { get; set; }
        public string Url { get; set; }
        public IFormFile Img { get; set; }
        public Guid? PageId { get; set; }
    }

when i receive the pageModel all the data are as expected even for the ImageModel properties like the Url and PageId exept for the Img IFormFile which is always null.

when i add:

var files = Request.Form.Files;

inside my post request, files contains all the missing Img files.

is their a way to bind the files directly to the IFormFile Img property?

the api request is called from an angular client using FormData.

createPage(pageModel : PageModel): Observable<PageModel> {
        var formData = new FormData();
        formData.append('title', pageModel .title); 

        formData.append('[image][pageId]', pageModel.pageId);
        formData.append('[image][url]', pageModel .image.url);
        formData.append('[image][img]', pageModel .image.img, pageModel.image.img.name);

        return this.http.post<PageModel>(this.Url, formData)
            .pipe(
                catchError(this.handleError)
            );
    }
0 Answers
Related