title: Javascript Windows.Location pass complex JSON object
body: <p>I've the following object which I am passing into an MVC Controller: </p>
  
  <pre><code>this.JsonData = {
      "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val(),
      "searchMode": {
          "mode": Number(mode.val()),
          "pageSize": Number(pagesize.val()) || 5,                                       "pageNumber": Number(pagenumber.val()) || 1,                
          "sortField": sortfield.val() || "Ref",
          "sortDirection": sortdirection.val() || "desc"
      },
      "searchData": {
          "Compare": Number(StdComparison.val()),
          "SearchTextFrom": searchText.val(),
          "SearchTextTo": searchTextTo.val()
      }
  </code></pre>
  
  <p>This works ok, but I recent requirement has arisen whereby I am looking to encode this object for use with the javascript function Window.location</p>
  
  <p>Suggestions I have used:</p>
  
  <p><a href="https://stackoverflow.com/questions/23153875/how-to-pass-complex-json-object-in-url-using-javascript">how-to-pass-complex-json-object-in-url-using-javascript</a></p>
  
  <pre><code>window.location + "?SearchCriteria=" + JSON.Stringify(this.JsonData);
  </code></pre>
  
  <p>Creates the following request:</p>
  
  <pre><code>Controller/Action?SearchCriteria={
      "__RequestVerificationToken": "tokenvalue",
      "searchMode": {
          "mode": 2,
          "pageSize": 5,
          "pageNumber": 1,
          "sortField": "Ref",
          "sortDirection": "desc"
      },
      "searchData": {
          "Compare": 1,
          "SearchTextFrom": "From A",
          "SearchTextTo": "To Z"
      }
  }
  </code></pre>
  
  <p>Whereas</p>
  
  <pre><code>window.location + "?SearchCriteria=" + this.JsonData;
  </code></pre>
  
  <p>Produced the following:</p>
  
  <pre><code>Controller/Action?SearchCriteria=[object%20Object]
  </code></pre>
  
  <p>With a page not found error on both of the above.</p>
  
  <p>UPDATE:</p>
  
  <p>I have moved forward in the quest for an answer.</p>
  
  <p>OK as per request from helpers, I have included more source code.</p>
  
  <p>I have Three Classes.</p>
  
  <pre><code>public class MainSearch
  {
      public MainSearch()
      {
          SearchData searchData = new SearchData();
          SearchMode searchMode = new SearchMode();
      }
      public SearchData searchData { get; set; }
      public SearchMode searchMode { get; set; }
      public int? page { get; set; }
      public object ToPagedListParameters(int pagenumber)
      {
          searchMode.pageNumber = pagenumber;
          return page;
      }
      public IList&lt;string&gt; ValidationErrorMessages { get; set; }
  }
  public class SearchData
  {
      // Fields used for the ticket number search
      public int? ticketNumberCompare { get; set; }
      public string ticketSearchTextFrom { get; set; }
      public string ticketSearchTextTo { get; set; }
  
  }
  public class SearchMode
  {
      public int? mode { get; set; }
      public int? pageNumber { get; set; }
      public int? pageSize { get; set; }
      public string sortDirection { get; set; }
      public string sortField { get; set; }
      public string userURN { get; set; }
      public string __RequestVerificationToken { get; set; }
  }
  </code></pre>
  
  <p>These classes hold the criteria used for searching <em>(SearchData has been truncated)</em> </p>
  
  <p>The following is my controller code:</p>
  
  <pre><code>[HttpGet]
  public ActionResult DownloadFileCSV(MainSearch search)
  {
      string fileName = Server.MapPath("~/Content/Pdf/") + "somefile.pdf";
      byte[] fileContents = System.IO.File.ReadAllBytes(fileName);
      return File(fileContents, "application/pdf", "result.pdf");
  }
  </code></pre>
  
  <p>And finally, the Ajax call that is made from the cshtml file.</p>
  
  <pre><code>$("#DownloadAttachmentCSV").click(function () {
      $.ajax(
          {
              url: '@Url.Action("DownloadFileCSV", "Home")',
              contentType: 'application/json; charset=utf-8',
              datatype: 'json',
              data: JsonData,  
              type: "GET",
              success: function () {
                  window.location = '@Url.Action("DownloadFileCSV", "Home")' + '?' + JsonData;                      
              },
              error: function (xhr, ajaxOptions, thrownError) {
                  alert(xhr.status);
                  alert(thrownError);
              }
          });
      });
  </code></pre>
  
  <p>Oddly enough, the code above does actually work and the file is downloaded, but here is the problem. The JSON data is not populating the MainSearch variable.</p>
  
  <p>So far the only way I can get the JSON data to populate the c# classes is to change the method to a POST. </p>
  
  <p>Must this be the case?</p>