Note that there are some explanatory texts on larger screens.

plurals
  1. POSend array to MVC controller via JSON?
    primarykey
    data
    text
    <p>I am trying, and struggling, to send an array via JSON to a MVC controller action.</p> <p>Here is what I have and what i've tried...</p> <pre><code>//Get checked records var $checkedRecords = $(':checked'); //e.g. 3 rows selected = [input 4, input 5, input 6] //Have tried following: sendingVar: $checkedRecords.serializeArray(); // gives array of 0's sendingVar: JSON.stringify($checkedRecords); // gives "{\"length\":1,\"prevObject\":{\"0\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"context\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"length\":1},\"context\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"selector\":\":checked\",\"0\":{}}"...wtf //Post $.post(url, { sendingVar: sendingVar }, function(data) {alert(data); }); </code></pre> <p>How do I do it ?</p> <p>edit: to those that suggest sending <code>$checkedRecords</code> "as is" from the top line - that is not working. I get a strange exception somewhere in the jquery framework :(</p> <pre><code>uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://.../.../.../jquery-1.4.4.min.js :: &lt;TOP_LEVEL&gt; :: line 141" data: no] </code></pre> <p>which I think means it is attempting to assign null to something it cannot.</p> <p>Edit: i'm using MVC2 not 3</p> <p>Edit2: After @Monday's answer- <strong>the problem is due to how I have built the array like</strong> <code>[input 4, input 5, input 6]</code> and not <code>[4,5,6]</code> - any ideas how I can just get the values in the array instead ?</p> <p>Edit3: Stop voting up duplicate when it's not. Did you actually read my problem or read the problems linked? this is a different issue</p> <p>@Daveo:</p> <p><a href="https://stackoverflow.com/questions/320291/how-to-post-an-array-of-complex-objects-with-json-jquery-to-asp-net-mvc-controll">I don't want to build in an overriding custom attribute just to send an array from JSON, that is rediculous</a> as we've already covered in this question-it is not necessary.</p> <p><a href="https://stackoverflow.com/questions/4789481/post-an-array-of-objects-via-json-to-asp-net-mvc3">MVC3 - irrelevant</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    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