嵌套集合 (Dictionary<int, List<string>>) 没有正确传递给查询字符串

问题描述

我有一个 post 操作,成功后会填充一个新的视图模型:ConfirmationViewModel.cs。这个视图模型包含一个名为 Wings 的属性,它只不过是一个以 int 为键类型,List<string> 为值的字典。成功地将所需的数据传递给这个视图模型后,该动作重定向到另一个动作,同时传递视图模型。

然而,正如您在下图中所见,在下一个请求中未成功填充字典。因此,我怀疑在将查询字符串参数绑定到视图模型的过程中出现了问题。

Debug info

然而,查询字符串显示了真正的问题:

https://localhost:44341/ShipRegistration/Confirmation?Hull=Neptunus&Engine=Galaxy%20Class&Wings=%5B1,%20System.Collections.Generic.List%601%5BSystem.String%5D%5D&Wings=%5B2,%20System.Collections.Generic.List%601%5BSystem.String%5D%5D&TotalWeight=908&TotalEnergy=186

或 URL 解码:

https://localhost:44341/ShipRegistration/Confirmation?Hull=Neptunus&Engine=Galaxy Class&Wings=[1,System.Collections.Generic.List`1[System.String]]&Wings=[2,System.Collections.Generic.List`1[System.String]]&TotalWeight=908&TotalEnergy=186

不知何故,嵌套集合的值没有正确传递给查询字符串。相反,仅传递对象的类型 (System.Collections.Generic.List)。

我知道在这一点上使用会话会让我的生活更轻松,但不幸的是,使用会话或任何其他形式的持久性对我来说不是一种选择。

希望有人能帮我解决这个问题。

提前致谢,

瑞恩

解决方法

这是在查询字符串中传递字典的演示:

控制器:

public IActionResult TestDictionary(ConfirmationViewModel c) 
        {
            return Ok();
        }

确认视图模型:

public class ConfirmationViewModel
    {
        public Dictionary<int,List<string>> Wings { get; set; }
        public string Engine { get; set; }
        public string Hull { get; set; }
        public int TotalEnergy { get; set; }
        public int TotalWeight { get; set; }

    }

网址:

https://localhost:xxx/xxx/xxx?Hull=Neptunus&Engine=Galaxy%20Class&TotalWeight=908&TotalEnergy=186&Wings.1=s&Wings.1=ss&Wings.2=r&Wings.2=rr

结果: enter image description here

您也可以像这样使用网址:

https://localhost:xxx/xxx/xxx?Hull=Neptunus&Engine=Galaxy%20Class&TotalWeight=908&TotalEnergy=186&Wings[1]=s&Wings[1]=ss&Wings[2]=r&Wings[2]=rr

https://localhost:xxx/xxx/xxx?Hull=Neptunus&Engine=Galaxy%20Class&TotalWeight=908&TotalEnergy=186&Wings[1][0]=s&Wings[1][1]=ss&Wings[2][0]=r&Wings[2][1]=rr

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...