Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inifinite loop during model binding when using [ModelBinder] attribute

I initially posted this issue to GitHub here: https://github.com/aspnet/Mvc/issues/8723

There's a GitHub repository with a repro of the problem here: https://github.com/Costo/aspnetcore-binding-bug

I'm using ASP.NET Core 2.2 Preview 3.

When using a custom model binder (with the [ModelBinder] attribute) on properties of an array of "child" models, the model binding phase of the request goes into an infinite loop. See this screenshot:

Model binding looping infinitely

The custom model binder works well if used on top level model properties, but I'd like to understand why it doesn't work when used in an array of child models. Any help with this would be appreciated.

Thank you !


Here's the code of the model, controller, view and custom binder:

The Model:

public class TestModel
{
    public TestInnerModel[] InnerModels { get; set; } = new TestInnerModel[0];

    [ModelBinder(BinderType = typeof(NumberModelBinder))]
    public decimal TopLevelRate { get; set; }
}

public class TestInnerModel
{
    public TestInnerModel()
    {
    }

    [ModelBinder(BinderType = typeof(NumberModelBinder))]
    public decimal Rate { get; set; }
}

The custom model binder (intentionally simplified to do nothing special):

public class NumberModelBinder : IModelBinder
{
    private readonly NumberStyles _supportedStyles = NumberStyles.Float | NumberStyles.AllowThousands;
    private DecimalModelBinder _innerBinder;

    public NumberModelBinder(ILoggerFactory loggerFactory)
    {
        _innerBinder = new DecimalModelBinder(_supportedStyles, loggerFactory);
    }

    /// <inheritdoc />
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        return _innerBinder.BindModelAsync(bindingContext);
    }
}

The controller:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View(new TestModel
        {
            TopLevelRate = 20m,
            InnerModels = new TestInnerModel[]
            {
                new TestInnerModel { Rate = 2.0m },
                new TestInnerModel { Rate = 0.2m }
            }
        });
    }

    [HttpPost]
    public IActionResult Index(TestModel model)
    {
        return Ok();
    }
}

The Razor view:

@model TestModel;

<form asp-controller="Home" asp-action="Index" method="post" role="form">
    <div>
        <input asp-for="@Model.TopLevelRate" type="number" min="0" step=".01" />

    </div>
    <div>
        @for (var i = 0; i < Model.InnerModels.Length; i++)
        {
            <input asp-for="@Model.InnerModels[i].Rate" type="number" min="0" step=".01" />
        }
    </div>

    <input type="submit" value="Go" />
</form>
like image 252
Costo Avatar asked Oct 30 '25 01:10

Costo


1 Answers

A solution was posted to the GitHub issue:

@Costo The problem is you're not informing the model binding system that the binder uses value providers. The ComplexTypeModelBinder always believes data is available for the next TestInnerModel instance and the outermost binder (CollectionModelBinder) keeps going -- forever. To fix this,

[MyModelBinder]
public decimal Rate { get; set; }

private class MyModelBinderAttribute : ModelBinderAttribute
{
    public MyModelBinderAttribute()
        : base(typeof(NumberModelBinder))
    {
        BindingSource = BindingSource.Form;
    }
}

Put another way, the BindingSource.Custom default [ModelBinder] uses isn't correct in this scenario. Fortunately custom model binders on properties of POCO types in containers should be one of the very few cases where this matters.

like image 51
Costo Avatar answered Nov 02 '25 23:11

Costo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!