Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Name { 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.

like image 853
Ben Foster Avatar asked Oct 10 '22 06:10

Ben Foster


1 Answers

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.

like image 86
Buildstarted Avatar answered Oct 14 '22 02:10

Buildstarted