列表 <string> 的模型绑定因只有一个元素的列表而失败

Model Binding of List<string> Fails with List of One Element

我有将列表或字符串传递给控制器​​动作的代码,只要列表中有 2 个或更多项目,它就可以完美运行。如果列表中只有一个项目,控制器的参数将解析为 null(我认为是模型绑定失败的结果)。

例如,如果我的 Controller Action 有一个签名,例如

public ActionResult myAction(List<string> myList)

我有一个 Uri,例如

someUri?mylist=first&mylist=second&mylist=third 

工作得很好,但是使用

这样的 Uri
someurl?mylist=first

参数 myList 解析为空。我是否必须将空白值附加为列表中的第二项,或者是否有其他方法强制模型活页夹正确绑定?

更新,这里有一些文字值:

public void PDFOutput(Guid MyProtocolsId, List<string> AssociationTypes)

这个有效:

http://localhost:50024/MyProtocols/FullPrint/642ff9cd-fb32-4a79-aaa4-088278796bb0?AssociationsTypes=Problem&AssociationTypes=Goal&AssociationTypes=StandardOrder&AssociationTypes=Outcome

这不是:

http://localhost:50024/MyProtocols/FullPrint/642ff9cd-fb32-4a79-aaa4-088278796bb0?AssociationsTypes=Problem

路由配置:

        routes.MapRoute(
            name: "MyProtocols",
            url: "MyProtocols/{action}/{myProtocolsId}",
            defaults: new { controller = "MyProtocols", action = "Index", myProtocolsId = UrlParameter.Optional }

        );

当您处理列表时,您可以使用索引作为查询字符串传递列表对象,如下所示。

http://localhost:50024/MyProtocols/FullPrint/642ff9cd-fb32-4a79-aaa4-088278796bb0?[0]=Problem

我试过了,效果如下。