gpt4 book ai didi

asp.net-mvc-3 - 为什么asp.net mvc模型绑定(bind)器产生system.string[]

转载 作者:行者123 更新时间:2023-12-04 20:20:28 24 4
gpt4 key购买 nike

我有一个表单,可以将有效的对象字典发布到我的 Controller 操作中。所以我们得到一个 IEnumerable<EditThemeAttributeModel>

        public class EditThemeAttributeModel
{
public string Name { get; set; }
public object Value { get; set; }
}

当我查看 Request.Form 集合时,我看到了我的期望:
    [1] "Attributes[0].Name"    string
[2] "Attributes[0].Value" string
[3] "Attributes[1].Name" string
[4] "Attributes[1].Value" string
[5] "Attributes[2].Name" string
[6] "Attributes[2].Value" string

但是,当我尝试获取 EditThemeAttributeModel 之一的值时它不是我期望的简单类型,它是一个字符串数组:
-   Value   {string[1]} object {string[]}
[0] "#ffffff" string

我可以通过直接使用 Request.Forms 集合来解决这个问题,但只是想了解这种行为。

最佳答案

默认情况下,来自 http 请求的所有帖子都是字符串。由于您引用的是对象而不是类型化的对象,因此它默认为字符串,因为来自浏览器的数据是字符串。如果您希望它不是字符串,我建议您键入您的对象,或者您可以为 EditThemeAttributeModel 创建自己的模型绑定(bind)器。 .

关于asp.net-mvc-3 - 为什么asp.net mvc模型绑定(bind)器产生system.string[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com