Note that there are some explanatory texts on larger screens.

plurals
  1. POWebApi modelBinding fails when property inherits from Dictionary<string, string>
    primarykey
    data
    text
    <p>WebApi model binding fails when a property in the viewmodel <strong>inherits</strong> from <code>Dictionary&lt;string, string&gt;</code>. The same model binding works when the data type of the property is changed to just <code>Dictionary&lt;string, string&gt;</code>.</p> <p>Below is an example of the binding not working.</p> <p>When I submit the webpage data to the ApiController the binding will not bind the <strong><code>MyDictionary&lt;string, string&gt;</code></strong> ExtraData property. If I change the property to <strong><code>Dictionary&lt;string, string&gt;</code></strong> ExtraData property then Api controller will bind correctly. However this is not an option because the MyDictionary class has additional functionality that I need.</p> <p>I have a class that inherits from Dictionary<code>&lt;&gt;</code> that looks like the one below:</p> <pre><code>public class MyDictionary&lt;TKey, TVal&gt; : Dictionary&lt;TKey, TVal&gt; { } </code></pre> <p>I have a class that has a property of the type <strong>MyDictionary</strong></p> <pre><code>public class HomeModel { public string Name{ get; set; } public MyDictionary&lt;string, string&gt; ExtraData { get; set; } public HomeModel() { ExtraData = new MyDictionary&lt;string, string&gt;(); } } </code></pre> <p>I have a ApiController like this (it does NOT bind correctly. <strong>ExtraData.Count always returns zero</strong>):</p> <pre><code>public class HomeApiController : ApiController { // POST api/homeapi [HttpPost] [ActionName("Complex")] public string PostComplex([FromBody]HomeModel model) { return string.Format("vm.ExtraData.Count = {0}", model.ExtraData.Count); } } </code></pre> <p>The html form snippet looks like this:</p> <pre><code> &lt;ul&gt; &lt;li&gt; &lt;input type="text" name="ExtraData[0].Key" value="Key1"/&gt; &lt;input type="text" name="ExtraData[0].Value" value="Value1"/&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="text" name="ExtraData[1].Key" value="Key2"/&gt; &lt;input type="text" name="ExtraData[1].Value" value="Value2"/&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="text" name="ExtraData[2].Key" value="Key3"/&gt; &lt;input type="text" name="ExtraData[2].Value" value="Value3"/&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Any ideas as to why model binding does not work with an inherited property of <code>Dictionary&lt;string, string&gt;</code>?</p> <p>Data is being submitted to ApiController through a simple post as follows:</p> <pre><code> var sendFormData = function (addr) { var jqxhr = $.post(addr, $('form').serialize()) .success(function (x) { alert(x); }) .error(function () { alert("Failure"); }); return false; }; $("#submitWebApi").click(function () { $("form").submit(sendFormData('api/homeapi/complex')); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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