DefaultModelBinder:IList 与 List

发布于 2024-08-05 22:36:16 字数 386 浏览 4 评论 0原文

我不确定这是一个错误还是一个功能。我有一个操作参数,它采用带有一些字符串属性的 ListRequest 对象。 .NET MVC 尽职尽责地将同名的查询字符串参数映射到 ListRequest 对象的属性。

我添加了一个 ListRequest.Filters 属性,它是从查询字符串中获取的字符串列表: ?filter=foo&filter=bar

如果我将 .Filters 声明为 List(Of String) 类型的 Get/Set,DefaultModelBinder 会完全执行此操作你会期望什么。但是,如果我将 .Filters 声明为 IList(Of String) 的 Get/Set,DefaultModelBinder 会完全停止将值绑定到该属性。

这是一个功能还是一个错误?

I'm not sure if this is a bug, or a feature. I have an action param that takes a ListRequest object with a few string properties. .NET MVC dutifully maps the query string params of the same name to the ListRequest objects' properties.

I add a ListRequest.Filters property, which is to be a list of strings taken from the querystring: ?filter=foo&filter=bar

If I declare .Filters as a Get/Set of type List(Of String), DefaultModelBinder does exactly what you would expect. However, if I declare .Filters as a Get/Set of IList(Of String) instead, DefaultModelBinder stops binding values to that property completely.

Is this a feature, or a bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-08-12 22:36:16

对我来说听起来像是一个功能。模型绑定器需要绑定到具体类型。

如果你告诉它绑定到一个接口,它就不能做任何事情,因为它无法实例化要绑定的接口。

编辑:有趣

从源代码来看,它似乎会绑定到 IEnumerable、ICollection、IList 或 IDictionary 的泛型类型的模型,但它不会绑定到泛型类型的模型属性。

所以我不会说这是一个错误......我只是说这是他们忽略的一个功能。 :-)

Sounds like a feature to me. The model binder needs concrete types to bind to.

If you tell it to bind to an interface it can't do anything because it can't instantiate an interface to bind to.

EDIT: Interesting

Judging by the source code, it seems that it will bind to a model that is a generic type of IEnumerable, ICollection, IList or IDictionary BUT it won't bind on a model's property that is of a generic type.

So I wouldn't say it's a bug... I'd just say that it's a feature that they have overlooked. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文