Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, I solved my problem and this might be an important insight for other programmers as well, because I learnt something while solving my own problem.</p> <p><strong>My solution:</strong> I resorted to not using JSON and instead used the default encoding that HTTP uses for encoding form posted values, and then I used something that smacks of custom model binding (without actually creating a model binder).</p> <p><strong>Explanation:</strong> Normally, when you make an ajax request and pass data from the client to any server platform, if you do not specify the encoding, i.e. the <code>contentType</code> parameter in the settings object of jQuery's ajax method (or if you are using any other means to make the ajax request other than jQuery, then however that thing sets the <code>ContentType</code> HTTP header is what you're after), HTTP encodes your posted data using its default encoding, which is much like what you post with a GET request after the query string, only it is binary encoded and not sent as a part of the URL.</p> <p>If that's the case, and you're posting a collection of any type (<code>IList</code>, <code>IEnumerable</code>, <code>ICollection</code>, <code>IDictionary</code>, etc.) to ASP.NET MVC, then don't create an associative array in JavaScript to embody the collection. Don't even create an array. Don't create anything.</p> <p>In fact, just pass it along inside the main big object using the convention:</p> <pre><code>data = { /*scalar property */ Gar: 'har', /* collection */ 'Foo[index++].Bar' : 'value1', 'Foo[index++].Bar' : 'value2' } </code></pre> <p>And don't use JSON. That will solve half your problem.</p> <p>On the server side, receive the posted data in a <code>FormCollection</code> and use its <code>IValueProvider</code> methods (<code>GetValue</code>) to dig out the data you need. You don't have to explicitly create a model binder to do it. You can do it in a private method of your controller itself.</p> <p>I will improve this answer a bit later when I can find more time.</p>
    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.
    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