title: Iterating through list obtained via JSON using jQuery in ASP.NET MVC
body: <p>I am trying to write an autocomplete feature on a page where the user can enter characters into a textbox and the dropdown will give options for addresses which it retrieves that match the characters being typed. I have a method in the controller which gets the list of customer addresses(which is what I want to show in the textbox) by connecting to the database and executing a query with Linq which looks like the following:</p>
  
  <pre><code>public ActionResult _getCustomerAddresses(int customerId)
      {
          return Json(theShop.getCustomerAddresses(customerId).Select(s =&gt; new { AddressID = s.AddressID, Addr = s.FirstName + ": " + s.Address1 }).ToList());
      }
  </code></pre>
  
  <p>I then have a function in my loadAddresses method in my cshtml file which passes the currently logged in customers customerID to the _getCustomerAddresses method and looks like the following:</p>
  
  <pre><code> function loadAddresses() {
      $.ajax({
          type: 'post',
          url: "/ShoppingCart/_getCustomerAddresses?customer=@Model.LoggedInCustomerID",
          dataType: "json",
          success: function (data) {
              for (var line in data)
              {
                 //Need to somehow iterate through the data list and append them to an array so I can add them to the source within autocomplete method to display them.
              }
        }
      });
  }
  </code></pre>
  
  <p>How do I go through this JSON list that is returned from the controller method and append them to an array for displaying? (.autocomplete function of jQuery seems to require an array for the data to display <a href="https://jqueryui.com/autocomplete/" rel="nofollow">https://jqueryui.com/autocomplete/</a>) I may need a better explanation of using JSON to help me do this</p>
  
  <p>Thanks very much in advance,
  Corey</p>