DefaultModelBinder: IList vs List

我只是一个虾纸丫 提交于 2019-12-12 04:58:14

问题


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:


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



来源:https://stackoverflow.com/questions/1473904/defaultmodelbinder-ilist-vs-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!