question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IFormFile bind error when using in sub collection

See original GitHub issue

Describe the bug

When i trying to post form with sublist of images models with IFormFile properties, the program starts to hang! see the screen

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core ‘2.2’
  2. Run this code
public class CategoryCreateModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public int Priority { get; set; }
        public string Abbr { get; set; }
        public string Description { get; set; }
        public string LinkName { get; set; }
        public string ParentId { get; set; }
// here error binding
        public IList<AppImageEditModel> Images { get; set; }
    }

//uploaded images sub collection
public class AppImageEditModel
    {
        public string Id { get; set; }
        public int Priority { get; set; }
        public string Name { get; set; }
        public string ImageUrl { get; set; }
// if class contains IFormFile prop, hapends binding bug
        public IFormFile AppImageFile { get; set; }
        public AppImageType ImageType { get; set; }
        public bool IsMain { get; set; }
    }

// in controller action:
        [HttpPost]
        public async Task<IActionResult> Create(CategoryCreateModel model)
        {
            if (ModelState.IsValid)
            {
                await Mediator.Send(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }

No errors thrown, but frease executing: operative memory encreases from 100 megabytes to 3-4 gigabytes!!!

Screenshots when I submit form:

Increase memory screen Increase memory screen

Expected behavior

The execution is not come to action method and hangs

Binding in razor inside the form:

<form asp-action="Create" enctype="multipart/form-data"><input type="text" name="Images[0].Name" id="Images_0__Text" /> <input type="file" name="Images[0].AppImageFile" id="Images_0__AppImageFile" />

<input type="text" name="Images[1].Name" id="Images_1__Text" /> <input type="file" name="Images[1].AppImageFile" id="Images_1__AppImageFile"/>

Additional context

if you delete the property public IFormFile AppImageFile from class AppImageEditModel, the program will work well!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:26 (11 by maintainers)

github_iconTop GitHub Comments

9reactions
avanveelencommented, Dec 23, 2018

Last week I encountered the same issue, but also found a work around for it. So, for people facing the same issue and who can not wait for a fix:

When you set the index as a hidden value in the form post, the dotnet Core model binding does not not fall into an infinite loop. Like this:

<input type="hidden" name="Images.Index" value="0" />
<input type="text" name="Images[0].Name" />

<input type="hidden" name="Images.Index" value="1" />
<input type="text" name="Images[1].Name" />

I have made a small reproduction of the bug and included the work around.

4reactions
WahidBitarcommented, Jan 21, 2019

another solution different than @avanveelen workaround

you can just wrap the IFormFile in another class and it will work

    public class Company
    {
        public IList<Person> Employees { get; set; } = new List<Person>();
    }

    public class Person
    {
        public FormFileWrapper IdImage { get; set; }
    }

    public class FormFileWrapper
    {
        public IFormFile File { get; set; }
    }

then you don have to add the index in your html

<form action="http://localhost:5000/api/Values" method="post" enctype="multipart/form-data">
    <p><input type="file" name="Employees[0].IdImage.File"></p>
    <p><input type="file" name="Employees[1].IdImage.File"></p>
    <p><button type="submit">Submit</button></p>
</form>
Read more comments on GitHub >

github_iconTop Results From Across the Web

IFormFile bind error when using in sub collection #4802
When i trying to post form with sublist of images models with IFormFile properties, the program starts to hang! see the screen. To...
Read more >
IFormFile is not bound if nested in view model
Hello, I have the exact bug in .NET Core 3.1 with a small variation. Nesting IFormFile 'alone' results in a null in a...
Read more >
Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Read more >
Upload Single Or Multiple Files In ASP.NET Core Using ...
In this article, we are going to see how to upload files in asp.net core web application and store in root directory of...
Read more >
Exploring the model-binding logic of minimal APIs
In this post I look at how the RequestDelegateFactory.CreateArgument() chooses which source to use for model-binding and the logic involved.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found