title: Posting JSON Data to ASP.NET MVC
body: <p>Im trying to get a list of line items to a webpage using JSON, which will then be manipulated and sent back to the server by ajax request using the same JSON structure that arrived (except having had a field values changed). </p>
  
  <p>Receiving data from the server is easy, manipulation even easier! but sending that JSON data back to the server for saving... suicide time! PLEASE can someone help!</p>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>var lineitems;
  
  // get data from server
  $.ajax({
      url: '/Controller/GetData/',
      success: function(data){
          lineitems = data;
      }
  });
  
  // post data to server
  $.ajax({
      url: '/Controller/SaveData/',
      data: { incoming: lineitems }
  });
  </code></pre>
  
  <p><strong>C# - Objects</strong></p>
  
  <pre><code>public class LineItem{
      public string reference;
      public int quantity;
      public decimal amount;
  }
  </code></pre>
  
  <p><strong>C# - Controller</strong></p>
  
  <pre><code>public JsonResult GetData()
  {
      IEnumerable&lt;LineItem&gt; lineItems = ... ; // a whole bunch of line items
      return Json(lineItems);
  }
  
  public JsonResult SaveData(IEnumerable&lt;LineItem&gt; incoming){
      foreach(LineItem item in incoming){
          // save some stuff
      }
      return Json(new { success = true, message = "Some message" });
  }
  </code></pre>
  
  <p>The data arrives at the server as serialized post data. The automated model binder tries to bind <code>IEnumerable&lt;LineItem&gt; incoming</code> and surprisingly gets the resulting <code>IEnumerable</code> has the correct number of <code>LineItems</code> - it just doesnt populate them with data.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Using answers from a number of sources, primarily <code>djch</code> on another stackoverflow post and <code>BeRecursive</code> below, I solved my problem using two main methods.</p>
  
  <p><em>Server Side</em> </p>
  
  <p>The deserialiser below requires reference to <code>System.Runtime.Serialization</code> and <code>using System.Runtime.Serialization.Json</code></p>
  
  <pre><code>private T Deserialise&lt;T&gt;(string json)
  {
      using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
      {
          var serialiser = new DataContractJsonSerializer(typeof(T));
          return (T)serialiser.ReadObject(ms);
      }
  }
  
  public void Action(int id, string items){
      IEnumerable&lt;LineItem&gt; lineitems = Deserialise&lt;IEnumerable&lt;LineItem&gt;&gt;(items);
      // do whatever needs to be done - create, update, delete etc.
  }
  </code></pre>
  
  <p><em>Client Side</em></p>
  
  <p>It uses json.org's stringify method, available in this dependecy <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js" rel="nofollow noreferrer">https://github.com/douglascrockford/JSON-js/blob/master/json2.js</a> (which is 2.5kb when minified)</p>
  
  <pre><code>$.ajax({
      type: 'POST',
      url: '/Controller/Action',
      data: { 'items': JSON.stringify(lineItems), 'id': documentId }
  });
  </code></pre>