title: Using ajax to send JSON to an action, parameter is null (returns internal server error 500)
body: <p>I'm trying to use ajax and JSON to get a result back from an action, and where it's working perfectly fine on different pages/actions, this one just won't work.</p>
  
  <p>My Javascript:</p>
  
  <pre><code>var lookup = $("#textbox").val();
  
  $.ajax({
      url: '/Controller/GetId',
      type: 'POST',
      dataType: 'json',
      data: lookup,
      contentType: 'application/json; charset=utf-8',
      success: function (data) {
          result = data;
      }
  });
  </code></pre>
  
  <p>My action:</p>
  
  <pre><code>public ActionResult GetId(string lookup)
  {
      ActionResult result = View();
  
      Order order =
          db.Orders
              .Where(o =&gt; o.Code == lookup)
              .FirstOrDefault();
  
      result = Json(order.Id.ToString());
  
      return result;
  }
  </code></pre>
  
  <p>Now, I've seen the following situations, all of which I can't really explain:</p>
  
  <ol>
  <li>With the code as above, console.log will display everything fine, the Ajax method throws a 500 internal server error and my breakpoint in my action is <em>not</em> hit (like the action is not called or something?).</li>
  <li>With the code as above, except using <strong>JSON.stringify</strong> around lookup, my breakpoint <em>is</em> hit, but the parameter is NULL. When I continue to debug through the action, the debugger.exe will freeze and crash.</li>
  </ol>
  
  <p>I have a similar situation on a different page, it posts a list as JSON.stringify, my action saves it to the db and returns a result message. This works perfectly fine! I'm completely stumped as to why the above won't work.</p>
  
  <p><strong>Edit</strong>
  I've narrowed it down (just a little bit): turns out the Error 500 Internal Server Error was being thrown because the code inside my action did not handle a null value correctly. Since the parameter is always null, because the JSON isn't sent (correctly), it would always break/crash. I've added a catch now, so JSON will correctly return an error now (saying that my order could not be found).</p>
  
  <p><strong>Edit2</strong>
  I'm not sure why this situation doesn't work, while my other does. But, the solution was to pass a model instead of a plain string and using stringify on the entire model. Finally, my action needed to accept a model as well:</p>
  
  <pre><code>var lookup = { lookup: $("#textbox").val() };
  
  $.ajax({
      url: '/Controller/GetId',
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(lookup),
      contentType: 'application/json; charset=utf-8',
      success: function (data) {
          result = data;
      }
  });
  
  public ActionResult GetId(Order model)
  {
      ActionResult result = View();
  
      Order order =
          db.Orders
              .Where(o =&gt; o.Lookup == model.Lookup)
              .FirstOrDefault();
  
      result = Json(order.Id.ToString());
  
      return result;
  }
  </code></pre>