Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pass a Dictionary as a parameter to an ActionResult method from jQuery/Ajax?
    primarykey
    data
    text
    <p>I'm using jQuery to make an Ajax call using an Http Post in ASP.NET MVC. I would like to be able to pass a Dictionary of values.</p> <p>The closest thing I could think of was to pass in a multi-dimensional array of strings, but the result that actually gets passed to the ActionResult method is a single dimensional string array containing a string concatenation of the "key/value" pair.</p> <p>For instance the first item in the below "values" array contains the below value:</p> <pre><code>"id,200" </code></pre> <p>Here's an example of my ActionResult method:</p> <pre><code>public ActionResult AddItems(string[] values) { // do something } </code></pre> <p>Here's an example of how I'm calling the method from jQuery:</p> <pre><code>$.post("/Controller/AddItems", { values: [ ["id", "200"], ["FirstName", "Chris"], ["DynamicItem1", "Some Value"], ["DynamicItem2", "Some Other Value"] ] }, function(data) { }, "json"); </code></pre> <p>Does anyone know how to pass a Dictionary object from jQuery to the ActionResult method instead of an Array?</p> <p>I would really like to define my ActionResult like this:</p> <pre><code>public ActionResult AddItems(Dictionary&lt;string, object&gt; values) { // do something } </code></pre> <p>Any suggestions?</p> <p><strong>UPDATE:</strong> I tried passing in a comma within the value and it basically just makes it impossible to actually parse the key/value pair using string parsing.</p> <p>Pass this:</p> <pre><code>values: [ ["id", "200,300"], ["FirstName", "Chris"] ] </code></pre> <p>results in this:</p> <pre><code>values[0] = "id,200,300"; values[1] = "FirstName,Chris"; </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.
 

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