title: How can I handle either an array or a single object with the same MVC controller method?
body: <p>I have an ActionResult method that accepts a list as an argument:</p>
  
  <pre><code>[HttpPost]
  public ActionResult MyMethod (List&lt;ClassA&gt; json)
  {
     ...
  }
  </code></pre>
  
  <p>This binds the json string coming in to a generic list of ClassA objects, which are populated.</p>
  
  <p>The problem is sometimes the json coming in is just a single json object and not an array of json objects.  </p>
  
  <p>Is there some way to preempt this so I can bind directly to ClassA vs List?  Or is there some other technique I can use?</p>
  
  <p>Here is how the JSON is being sent (as an array):</p>
  
  <pre><code>var myjsonarray = [{
    "ID": "1",
    "FirstName": "John",
    "LastName": "Doe",
  }, {
    "ID": "2",
    "FirstName": "Jane",
    "LastName": "Doe",
  }];
  
  $.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   url: "/Home/MyPage",
   data: JSON.stringify(myjsonarray),
   dataType: 'json'
  });
  </code></pre>
  
  <p>The above processes fine.  This also works: </p>
  
  <pre><code>var myjsonarray = [{
    "ID": "1",
    "FirstName": "John",
    "LastName": "Doe",
  }];
  </code></pre>
  
  <p>But when I send as a single object not wrapped in an array:</p>
  
  <pre><code>var myjsonarray = {
    "ID": "1",
    "FirstName": "John",
    "LastName": "Doe",
  };
  </code></pre>
  
  <p>My ActionResult method parameter is null:</p>
  
  <pre><code>json == null
  </code></pre>