DefaultModelBinder:IList的名单VS [英] DefaultModelBinder: IList vs List

查看:160
本文介绍了DefaultModelBinder:IList的名单VS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是一个bug,或者功能。我有一个动作参数是需要有一些字符串属性ListRequest对象。 .NET MVC尽职尽责地映射查询字符串PARAMS同名的ListRequest对象的属性。

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.

我添加了一个ListRequest.Filters属性,它是从查询字符串取一个字符串列表:过滤器=富&安培;过滤器=栏

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

如果我宣布.Filters作为一个获取/设置类型名单(串),DefaultModelBinder不正是你所期望的。不过,如果我宣布.Filters作为一个获取/设置的IList(串)来代替,DefaultModelBinder停止绑定值该属性完全。的

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?

推荐答案

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

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.

编辑:有趣

由源$ C ​​$ C来看,它似乎将绑定到一个模型,它是一个通用型的IEnumerable,ICollection的,IList的或IDictionary的,但它不会是一个通用型的模型的属性绑定

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

这篇关于DefaultModelBinder:IList的名单VS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆