微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决嵌套集合 (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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。