DefaultModelBinder: IList vs List

2019-08-26 12:43发布

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?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-26 13:29

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. :-)

查看更多
登录 后发表回答