gpt4 book ai didi

asp.net-core-mvc - 使用 [ModelBinder] 属性时模型绑定(bind)期间的无限循环

转载 作者:行者123 更新时间:2023-12-04 01:50:37 24 4
gpt4 key购买 nike

我最初在 GitHub 上发布了这个问题:https://github.com/aspnet/Mvc/issues/8723

这里有一个 GitHub 存储库,其中包含该问题的重现:
https://github.com/Costo/aspnetcore-binding-bug

我正在使用 ASP.NET Core 2.2 Preview 3。

在“子”模型数组的属性上使用自定义模型绑定(bind)器(带有 [ModelBinder] 属性)时,请求的模型绑定(bind)阶段进入无限循环。看这个截图:

Model binding looping infinitely

如果在顶级模型属性上使用自定义模型绑定(bind)器效果很好,但我想了解为什么它在子模型数组中使用时不起作用。对此的任何帮助将不胜感激。

谢谢 !

这是模型、 Controller 、 View 和自定义绑定(bind)器的代码:

该模型:

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; }
}

自定义模型绑定(bind)器(特意简化为没什么特别的):
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);
}
}

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();
}
}

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>

最佳答案

一个 solution已发布到 GitHub 问题:

@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.

关于asp.net-core-mvc - 使用 [ModelBinder] 属性时模型绑定(bind)期间的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53303414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com