I'm trying to use IFormFile as a property in a nested ViewModel. I am running into issues trying to bind the ViewModel to the controller action at runtime. The AJAX request stalls and never reaches the action.
This conceptual question is in reference to my specific issue at IFormFile property in .NET Core ViewModel causing stalled AJAX Request
ViewModel:
public class ProductViewModel
{
public ProductDTO Product { get; set; }
public List<ProductImageViewModel> Images { get; set; }
}
Nested ViewModel:
public class ProductImageViewModel
{
public ProductImageDTO ProductImage { get; set; }
public IFormFile ImageFile { get; set; }
}
Action:
[HttpPost]
public IActionResult SaveProduct([FromForm]ProductViewModel model)
{
//save code
}
I am wondering if an IFormFile Property needs to be a direct property of the ViewModel you are binding to a controller action.
The IFormFile Documentation does not seem to answer my question.
This is a known issue that has been fixed in
v3.0.0-preview
and won't be merged into2.2.x
branch. See see #4802.When posting a form with
IList<Something> Something
whereSomething
has a property ofIFormFile
directly, it will result in an infinite loop. Because the model binding happens before the invocation of action method, you'll find that it never enters the action method. Also, if you inspect the Task Manager, you'll find that a crazy memory usage.To walk around it, as suggested by @WahidBitar, simply create a wrapper on the
IFormFile
so thatSomething
doesn't have aIFormFile
directly .As for your question itself, change your code as below:
Now your client side should rename the field name as below:
A Working Demo :