开发者

Why is the asp.net mvc model binder producing system.string[]

开发者 https://www.devze.com 2023-03-27 20:28 出处:网络
I\'ve got a form that is posting what is effectively an object dictionary to my controller action. So we get an IEnumerable<EditThemeAttributeModel>

I've got a form that is posting what is effectively an object dictionary to my controller action. So we get an IEnumerable<EditThemeAttributeModel>

        public class EditThemeAttributeModel
        {
            public string Nam开发者_C百科e { get; set; }
            public object Value { get; set; }
        }

When I look at the Request.Form collection I see what I would expect:

    [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

However, when I try an attempt to get the value of one of the EditThemeAttributeModel it isn't a simple type as I would expect, it's a string array:

-   Value   {string[1]} object {string[]}
                [0] "#ffffff"   string

I can work around this by using the Request.Forms collection directly, but just wanted to understand this behaviour.


All posts from http requests are, by default, strings. Since you're referencing an object rather than a typed object it defaults to string because the data coming from the browser is a string. If you want it to not be a string I suggest you type your object or you can create your own model binder for EditThemeAttributeModel.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号