title: MVC ajax json post to controller action method
body: <p>I am trying to achieve a JQuery AJAX call to a controller action method that contains a complex object as a parameter.
  I have read plenty blogs and tried several techniques learned from these. The key post on which I have constructed my best attempt code (below) is the stackoverflow post <a href="https://stackoverflow.com/questions/560575/asp-net-mvc-how-to-pass-json-object-from-view-to-controller-as-parameter">here </a>.</p>
  
  <p>I want to trigger an asynchronous post, invoked when the user tabs off a field [not a Form save post – as demonstrated in other examples I have found]. </p>
  
  <p>My intention is to:</p>
  
  <ul><li>Instantiate an object on the client [not the ViewModel which provides the type for the View];
  <li>Populate the object with data from several fields in the view; 
  <li>Convert this object to JSON; 
  <li>Call the controller action method using the jQuery.Ajax method, passing the JSON object.  
  </ul>
  
  <p>The results will be returned as a JSON result; and data will be loaded into fields in the view depending on results returned.</p>
  
  <p>The problems are: </p>
  
  <ul><li>If the action method is attributed with the HttpPost attribute, the controller Action method is not invoked (even though the AJAX call type is set to ‘POST’).
  <li>If the action method isattributed with HttpGet, the values of properties of the parameter are null
  <li>The ReadObject method throws the error: "Expecting element 'root' from namespace ''.. Encountered 'None' with name 'namespace'". 
  </ul>
  
  <p>Hopefully someone can help. Thanks. Code below:</p>
  
  <p><strong>Client js file</strong></p>
  
  <pre><code> var disputeKeyDataObj = {
       "InvoiceNumber": "" + $.trim(this.value) + "",
       "CustomerNumber": "" + $.trim($('#CustomerNumber').val()) + ""
    };
  
    var disputeKeyDataJSON = JSON.stringify(disputeHeadlineData);      
  
    $.ajax({
       url: "/cnr/GetDataForInvoiceNumber",
       type: "POST",
       data: disputeKeyDataJSON,
       dataType: 'json',
       contentType: "application/json; charset=utf-8",
       success: EnrichedDisputeKeyData(result)
    });
  </code></pre>
  
  <p><br>
  <strong>Action Filter and class for the type associated with the Action method parameter</strong>  </p>
  
  <pre><code> [DataContract]  
   public class DisputeKeyData  
   {  
      [DataMember(Name = "InvoiceNumber")]  
      public string InvoiceNumber { get; set; }
  
      [DataMember(Name = "CustomerNumber")]
      public string CustomerNumber { get; set; }
   }  
  </code></pre>
  
  <p><strong>Action method on the controller</strong>  </p>
  
  <pre><code>  //[HttpPost]
    [ObjectFilter(Param = "disputeKeyData", RootType = typeof(DisputeKeyData))]  
    public ActionResult GetDataForInvoiceNumber(DisputeKeyData disputeKeyData)  
    {  
       //Blah!  
       //....  
       return Json(disputeKeyData, JsonRequestBehavior.AllowGet);  
    }  
  </code></pre>