Note that there are some explanatory texts on larger screens.

plurals
  1. POHtmlAttributes dictionary not binding correctly
    primarykey
    data
    text
    <p>I have a model binding issue with an html helper I'm writing. I have declared a property on my Model to handle html attributes, In short;</p> <pre><code>public IDictionary&lt;string,object&gt; HtmlAttributes { get; set; } </code></pre> <p>I then render the following html as in <a href="http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx" rel="nofollow" title="Scott Hanselman">Scott Hanselman's post</a>;</p> <pre><code>&lt;input type="hidden" id="HtmlAttributes[0]_Key" name="HtmlAttributes[0].Key" value="align" /&gt; &lt;input type="hidden" id="HtmlAttributes[0]_Value" name="HtmlAttributes[0].Value" value="center" /&gt; </code></pre> <p>But on callback the DefaultModelBinder creates the value as a string array, such that the next time I render my html value;</p> <pre><code>_attribute.Value.ToString() </code></pre> <p>I get the following HTML;</p> <pre><code>&lt;td align="System.String[]"&gt;&lt;/td&gt; </code></pre> <p>Which is obviously a default ToString representation of a string array. The value is the first element!!</p> <p>It seems the default model binder is confused about the value type parameter being declared as object for the Dictionary. I was sure I was following convention by declaring my htmlAttributes as <code>Dictionary&lt;string,object&gt;</code>, as I observed in the Html Helpers source code. Am I missing something obvious here?</p> <p>EDIT:</p> <p>Just an update to give more info. The binding issue I'm seeing is as a result of an JQuery AJAX post <code>$.post</code> callback, where the data is being serialized using JQuery's .serialize(); On inspecting the data being sent, again all looks to be in order.</p> <pre><code>HtmlAttributes%5B0%5D.Key=align&amp;HtmlAttributes%5B0%5D.Value=center&amp; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload